Wednesday, October 21, 2015

Hibernate annotations Introduction

Friends Happy point for you:- Annotations are replacement for “xyz.hbm.xml” files

Let us learn few points regarding annotations in hibernate

Annotations are introduced in java along with JDK 1.5, annotations are used to
provide META data to the classes, variables, and methods of java

Annotations are given by SUN as replacement to the use of xml files in java

In hibernate annotations are given to replace hibernate mapping [ xml ] files

While working with annotations in hibernate, we do not require any mapping files, but
hibernate xml configuration file is must required

Hibernate borrowed annotations from java persistence API but hibernate it self doesn’t
contain its own annotations

Every annotation is internally an Interface, and the keywords starts with @ symbol

Hibernate Transient + Persistent + Detached Objects

Transient Object :
One newly created object of POJO class without having any relation with the database, means
never persistent, not associated with any Session object.

Persistent Object:
An object of a POJO class having relation with the database, associated with a unique Session
object

Detached Object:
An object of a POJO class Previously having relation with the database [persistent ], now not
associated with any Session object

Hibernate Object's Transient & Persistent states


Whenever an object of a POJO class is created then it will be in the Transient state
An object in a Transient state doesn’t represent any row of the database,

Means it is not associated with any Session object, No relation with the database, it is just a
normal object

 If we modify the data of a POJO class object, when it is in transient state then it doesn’t effect
on the database table

When the object is in persistent state, then it represent one row of the database,

In persistent state object is having associated with the unique Session

If we want to move an object from persistent to detached state, then we need to do either
close that session or need to clear the cache of the session

If we want to move an object from persistent state into transient state then we need to delete
that object permanently from the database

Tuesday, October 20, 2015

Criteria Query + Conditions To Criteria + criterion

Criteria Query is only for selecting the data from the database

Using Criteria query we can select complete objects only not partial objects

In fact by combining criteria and projections concept we can select partial objects too

We cannot perform non-select operations using this criteria.

Criteria is suitable for executing dynamic queries too,

Friend lets take a flight of how to use this criteria queries in the hibernate
Syntax:
Criteria criteria = session.createCriteria(“Class Name”.class);
How to use ?
Criteria criteria = session.createCriteria(Entity.class);
List list = criteria.list()
Iterator iterator = list.iterator();
while(iterator.hasNext())
{
Object obj = iterator.next();
Entity entity = (Entity)obj;
/*------ Further code ----- */
}

Adding Conditions To Criteria
If we want to put conditions to load data from database, using criteria then we need to create
one Criterion Interface object and we need to add this object to Criteria Class object

Criterion is an interface given in “org.hibernate.criterion” package

In order to get Criterion object, we need to use Restrictions class

Restrictions is the factory for producing Criterion objects, but friends there is no explicit
relation between Criterion interface and Restrictions class, it means Restrictions class is not
implemented from Criterion Interface

Restrictions class, we have all static methods and each method of this class returns
Criterion object

Restrictions class is also given in “org.hibernate.criterion” package

How to use criterion ?
Criteria criteria = session.createCriteria(Entity.class);
Criterion criterion =Restrictions.gt("id", new Integer(11));
// .gt() : means greater than
// id is a variable of our POJO class Entity.java
criteria.add(criterion); // adding criterion object to criteria class object
List l = criteria.list(); // executing criteria query
In our above example we are fetching the data by comparing id greater than (>) 11
If we want to put more conditions on the data (multiple conditions) then we can use
.and() method ,. or() method , .eq() method using the Restrictions class
How to use Restrictions?
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.and(Restrictions.like("name","%M%"),Restrictions.eq("category",ne
w String(“bird”))));
List list = criteria.list();
Iterator it = list.iterator();
Buddy, like this we can add required number of conditions

Conditions to Criteria
If we want to add some sorting order for the objects, before the objects are going to store
in list object then we need to add an Order class object to the Criteria class object by
calling addOrder() method

Order is a class given in “org.hibernate.Criterion” package

In Order class, we have 2 static methods, asc()[ascending order] and dsc()[descending
order] for getting an objects in required order

Internal concept is, hibernate will select the records (rows) from ENTITIES_DATA table
and stores them into a ResultSet and then converts each row data of resultset into a POJO
class object basing on our field type, then add all these objects into a list according to the
order we have given

Monday, October 19, 2015

JAVA ROBOT CODE TO DO SOME PREDEFINE ACTIVITY

THIS CODE WILL TAKE UPDATE IN YOUR PARTICULAR FOLDER FOR THE CONFIGURED WORKSPACE.

IT WILL OPEN YOUR PREDEFINE SOFTWARE (i.e. Eclipse, ThunderBird etc)

IT WILL OPEN YOUR SOME ROUTINE SITES (i.e Gmail, Hotmail etc etc)

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/*Author : RAJ*/

class MyRobot {

Robot robot = new Robot();

public static void main(String[] args) {

try {

Desktop desktop = Desktop.getDesktop();
File dirToOpen = new File("D:\\Data\\My_Branch");
desktop.open(dirToOpen);

MyRobot r = new MyRobot();

// Creates the delay of 5 sec so that you can open notepad before
// Robot start writting
/*
* robot.delay(5000); robot.keyPress(KeyEvent.VK_H);
* robot.keyPress(KeyEvent.VK_I); robot.keyPress(KeyEvent.VK_SPACE);
* robot.keyPress(KeyEvent.VK_B); robot.keyPress(KeyEvent.VK_U);
* robot.keyPress(KeyEvent.VK_D); robot.keyPress(KeyEvent.VK_Y);
*/

} catch (Exception e) {
e.printStackTrace();
}
}

public MyRobot() throws AWTException {
startTheBattle();
}

private void leftClick() {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(200);
}

private void startTheBattle(){
rightClick();
leftClick();
robot.delay(1000);

openChrome();
robot.delay(1000);

openEclipse();
robot.delay(1000);

openThunder();
exitNow();
}

private void exitNow() {
System.out.println("Exiting Now.. Good Bye..");
System.exit(0);
}

private void rightClick() {
robot.mouseMove(640, 630);
robot.delay(500);

robot.mousePress(InputEvent.BUTTON3_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON3_MASK);
robot.delay(200);

robot.mouseMove(660, 500);
robot.delay(500);
}

private void openChrome(){
try{
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://gmail.com"));
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://yahoomail.com"));
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://hotmail.com"));
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://google.com"));
}catch(Exception ex){}
}


private void openEclipse(){
try{
String path = "D:\\Data\\Software\\eclipse-jee-indigo-SR2-win32-x86_64 - Branch-660\\eclipse\\eclipse.exe";
File file = new File(path);
if (! file.exists()) {
throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());
}catch(Exception ex){}
}

private void openThunder(){
try{
String path = "D:\\Data\\Software\\ThunderbirdPortable\\ThunderbirdPortable.exe";
File file = new File(path);
if (! file.exists()) {
throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());
}catch(Exception ex){}
}

private void type(int i) {
robot.delay(40);
robot.keyPress(i);
robot.keyRelease(i);
}

private void type(String s) {
byte[] bytes = s.getBytes();
for (byte b : bytes) {
int code = b;
// keycode only handles [A-Z] (which is ASCII decimal [65-90])
if (code > 96 && code < 123)
code = code - 32;
robot.delay(40);
robot.keyPress(code);
robot.keyRelease(code);
}
}
}

what is difference between < ? super T > and < ? extends T >

what is difference between < ? super T> and <? extends T> ?

<? super T> :
Here the ? is a wildcard which represents the parameter type. (i.e List<Integer> here Integer is a parameter type), T is a generic Type and super is a keyword which represents the put principle.
If it is List<? super T> then one can put all the types objects into this list which comes int the range of ? and T.
because of the usage of super one cannot get the values from this list.

<? extends T>:
Here the ? is a wildcard which represents the parameter type. (i.e List<Character> here Character is a parameter type).
T is a generic type (i.e public static <T> void getList(T[] arr);  here T is a generic type)
and extends is keyword. and the pattern <? extends T> represets the get principle of generic.
means if it is List<? extends T> then one can get the values from this list. and the value object will be ranging between the ?(TYPE) and T(type).

 

how the Servlet Container Works

Creating a request object and populate it with information that may be used by the invoked servlet, such as parameters, headers, cookies, query string, URI, etc. A request object is an instance of the javax.servlet.ServletRequest interface or the javax.servlet.http.ServletRequest interface.

Creating a response object that the invoked servlet uses to send the response to the web client. A response object is an instance of the javax.servlet.ServletResponse interface or the javax.servlet.http.ServletResponse interface.

Invoking the service method of the servlet, passing the request and response objects. Here the servlet reads the values from the request object and writes to the response object.

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...