Tuesday, July 27, 2010

what is lazy fetching in hibernate?

Lazy setting decides whether to load child objects while loading the Parent Object.You need to do this setting respective hibernate mapping file of the parent class.Lazy = true (means not to load child)By default the lazy loading of the child objects is true. This make sure that the child objects are not loaded unless they are explicitly invoked in the application by calling getChild() method on parent.In this case hibernate issues a fresh database call to load the child when getChild() is actully called on the Parent object.But in some cases you do need to load the child objects when parent is loaded. Just make the lazy=false and hibernate will load the child when parent is loaded from the database.Exampleslazy=true (default)Address child of User class can be made lazy if it is not required frequently.lazy=falseBut you may need to load the Author object for Book parent whenever you deal with the book for online bookshop

Monday, July 26, 2010

Implementing Singletons

There are a few ways to implement Singletons. Although you can get Singleton-like behavior with static fields and methods [for example, java.lang.Math.sin(double)], you gain more flexibility by creating an instance. With Singletons implemented as single instances instead of static class members, you can initialize the Singleton lazily, creating it only when it is first used. Likewise, with a Singleton implemented as single instance, you leave open the possibility of altering the class to create more instances in the future. With some implementations of the Singleton, you allow writers of subclasses to override methods polymorphically, something not possible with static methods.

Most commonly, you implement a Singleton in Java by having a single instance of the class as a static field. You can create that instance at class-loading time by assigning a newly created object to the static field in the field declaration, as seen in Listing 1.

Listing 1
public class MySingleton {
private static MySingleton _instance =
new MySingleton();

private MySingleton() {
// construct object . . .
}

public static MySingleton getInstance() {
return _instance;
}

// Remainder of class definition . . .

Alternatively, you can instantiate it lazily, on first demand, as seen in Listing 2. You keep the constructor private to prevent instantiation by outside callers.

Listing 2
public class MySingleton {
private static MySingleton _instance;

private MySingleton() {
// construct object . . .
}

// For lazy initialization
public static synchronized MySingleton getInstance() {
if (_instance==null) {
_instance = new MySingleton();
}
return _instance;
}
// Remainder of class definition . . .
}

Both Singleton implementations do not allow convenient subclassing, since getInstance(), being static, cannot be overridden polymorphically. Other implementations prove more flexible. While I don't have the space to describe alternative implementations in detail, here are a couple of possibilities:

Creational Patterns

- Factory Pattern


Factory of what? Of classes. In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.

When to use a Factory Pattern?
The Factory patterns can be used in following cases:
1. When a class does not know which class of objects it must create.
2. A class specifies its sub-classes to specify which objects to create.
3. In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.



Abstract Factory Pattern


This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes. Like Factory pattern returned one of the several sub-classes, this returns such factory which later will return one of the sub-classes.


When to use Abstract Factory Pattern?
One of the main advantages of Abstract Factory Pattern is that it isolates the concrete classes that are generated. The names of actual implementing classes are not needed to be known at the client side. Because of the isolation, you can change the implementation from one factory to another.


Singleton Pattern:



Difference between static class and static method approaches:
One question which a lot of people have been asking me. What’s the difference between a singleton class and a static class? The answer is static class is one approach to make a class “Singleton”.

We can create a class and declare it as “final” with all the methods “static”. In this case, you can’t create any instance of class and can call the static methods directly.








Monday, July 19, 2010

Object Serialization And De-Serialization In Java

Implementing serialization in Java is pretty straight forward just a little step task. The class you want to be serialized just have to implements a marker Interface Serializable no override nothing extra work.

The JVM will take care of serialization and deserialization process automatically. Now you can write your object to any persistent technology e.g. database, file system or even over the network.

Serialization: The process of writing bits and bytes of a java object to physical stream.

Deserialization: The process of reading bits and bytes from physical stream to construct a java object.

Implementing serialization straight forward or lightly may cause problems in future. Let’s discuss some issues that can be faces.

If you not implement serialVersionUID, when you are saving the object JVM automatically calculate serialVersionUID depending upon the various class factors. So when deserialization reading object back it again calculate the serialVersionUID of the persistent object and to whom class it going to referenced. If class structure changed after saving object then a ClassCastException will be thrown.

JVM takes significant time to generate serialVersionUID at runtime, so having serialVersionUID generated will also improve the performance.

So always be sure to have serialVersionUID, follow the guidelines to implement serialVersionUID.

  • Always include it as field, “private static final long serialVersionUID = 1L”, include this field even in the first version of the class, as a reminder of its important.
  • Do not alter the value of this field in future versions, unless you are knowingly making changes to the class which will result in incompatible with all old serialized objects.
  • In Windows, generate serialVersionUID using JDK’s graphical tool like so: issue the like following
    serialver –show persistentClassName/Path
  • Most of modern IDEs can generate serialVersionUID, like in My Eclipse if you are implement Serializable interface, it will show a bulb sign clicking on it will indicate to generate serialVersionUID automatically by the IDE.

Thursday, May 6, 2010

imp question and ansewrs

http://www.interview-questions-java.com/java-threads.htm

Wednesday, April 28, 2010

List to XML

Iterator it = list.iterator();
while (it.hasNext()) {

Timesheet timesheet = (Timesheet) it.next();

Element row = doc.createElement("Row");
results.appendChild(row);
String columnName = null;

try {
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
row.appendChild(getElementNode(doc, "RES_ID", Integer
.toString(timesheet.getRES_ID())));

row.appendChild(getElementNode(doc, "FULL_NAME", timesheet
.getFULL_NAME()));

row.appendChild(getElementNode(doc, "RES_UNIQUE_NAME",
timesheet.getRES_UNIQUE_NAME()));

row.appendChild(getElementNode(doc, "RES_OPEN", Integer
.toString(timesheet.getRES_OPEN())));

row.appendChild(getElementNode(doc, "TS_START", sdf.format(
timesheet.getTS_START()).trim()));

row.appendChild(getElementNode(doc, "TS_FINISH", sdf
.format(timesheet.getTS_FINISH()).trim()));

row.appendChild(getElementNode(doc, "TP_ID", timesheet
.getTP_ID()));

row.appendChild(getElementNode(doc, "TP_OPEN", CommonUtils
.convertBooleanToString(timesheet.isTP_OPEN())));

row.appendChild(getElementNode(doc, "RESOURCE_TYPE",
CommonUtils.convertBooleanToString(timesheet
.isRESOURCE_TYPE())));

row.appendChild(getElementNode(doc, "TS_ID", Integer
.toString(timesheet.getTS_ID())));

row.appendChild(getElementNode(doc, "TS_VERSION", Integer
.toString(timesheet.getVersion())));

row.appendChild(getElementNode(doc, "TS_STATUS", Integer
.toString(timesheet.getTS_STATUS())));

row.appendChild(getElementNode(doc, "TS_META_STATUS",
Integer.toString(timesheet.getTS_META_STATUS())));

row.appendChild(getElementNode(doc, "TS_HOURS", Double
.toString(timesheet.getTS_HOURS())));

row.appendChild(getElementNode(doc, "TS_IS_ADJUSTMENT",
CommonUtils.convertBooleanToString(timesheet
.isTS_IS_ADJUSTMENT())));

row.appendChild(getElementNode(doc, "BEING_ADJ",
CommonUtils.convertBooleanToString(timesheet
.isBEING_ADJ())));

row.appendChild(getElementNode(doc, "TS_IS_ADJUSTED",
CommonUtils.convertBooleanToString(timesheet
.isTS_IS_ADJUSTED())));

} catch (Exception ex) {
Element node = doc.createElement(columnName.toUpperCase());
node.appendChild(doc.createTextNode(""));
row.appendChild(node);
}

}

} catch (Exception e) {

e.printStackTrace();

}
return doc;
}

Result set to List

while(rset.next()){

TaskListPojo taskList = new TaskListPojo();

taskList.setPROJECT_NAME(rset.getString("Col_1"));

taskList.setPROJECT_ID(rset.getString("Col_2"));

taskList.setPROJECT_CODE(rset.getString("Col_3"));

taskList.setSATRT_DATE(rset.getDate("Col_4"));

taskList.setFINISH_DATE(rset.getDate("Col_5"));

taskList.setTASK_NAME(rset.getString("Col_6"));

taskList.setTASK_ID(rset.getInt("Col_7"));

taskList.setETC(rset.getString("Col_8"));

taskList.setUNIQUE_NAME(rset.getString("Col_9"));

taskList.setTASK_STATUS(rset.getString("Col_10"));

taskListObj.add(taskList);

}