Thursday, 16 February 2012

How to Make Updates to Updatable Result Sets in JDBC? | Java JDBC


Another new feature in the JDBC 2.0 API is the ability to update rows in a result set using methods in the Java programming language rather than having to send an SQL command. But before you can take advantage of this capability, you need to create a ResultSet object that is updatable. In order to do this, you supply the ResultSet constant CONCUR_UPDATABLE to the createStatement method.
E.g.
Connection con = DriverManager.getConnection(”jdbc:mySubprotocol:mySubName”);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = (”SELECT COF_NAME, PRICE FROM COFFEES”); 

How to Retrieve Warnings in JDBC? | Java JDBC


SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object
E.g.
SQLWarning warning = stmt.getWarnings();
if (warning != null) 
{
while (warning != null) 
{
System.out.println(”Message: ” + warning.getMessage());
System.out.println(”SQLState: ” + warning.getSQLState());
System.out.print(”Vendor error code: “);
System.out.println(warning.getErrorCode());
warning = warning.getNextWarning();
}
}

Explain the advantages and disadvantages of detached objects? | Java Hibernate

Advantages:
•    Detached objects passing can be done across layers upto the presentation layer without using Data Transfer Objects.
•    At the time of using long transactions by the user which needs long think-time, it is suggested to split these transactions into some transactions. The detached objects get modified apart from the transaction scope which then can be re-attached to a new transaction.
Disadvantages:
•    The usage of detached objects are cumbersome and cryptic. It is suggested not to be cluttered with the session, if possible.
•    It is recommended to use DataTransferObjects and DomainObjects that is used to maintain separation between the user interfaces and the Service.

What is the difference between sorted and ordered collection in hibernate? | Java Hibernate

Sorted Collection
The sorted collection is a collection that is sorted using the Java collections framework. The sorting is done in the memory of JVM that is running hibernate, soon after reading the data from the database using Java Comparator
The less the collection the more the efficient of sorting
Ordered Collection
The order collections will also sorts a collection by using the order by clause for the results fetched.
The more the collection, the more efficient of sorting.

What is lazy fetching in hibernate?

•    Lazy setting decides whether to load child objects while loading the Parent Object.
•    This can be done by a setting in hibernate mapping file of the parent class.Lazy = true
•    By default the lazy loading of the child objects is true.

What is lazy initialization in hibernate? | Java Hibernate

The delaying the object creation or calculating a value or some process until the first time it is needed. The retrieval of particular information only at the time when the object is accessed, is lazy initialization in hibernate. A scenario for lazy initialization is:
When the field creation is expensive, a field may or may not be invoked.
In this scenario the creation of a field can be deferred until the actual moment is arise to use it. The performance is increased using this technique, by avoiding unnecessary creation of objects which is expensive and consumes the memory space.

What are Collection types in Hibernate? | Java Hibernate

•    ArrayType,
Constructor: ArrayType(String role, String propertyRef, Class elementClass, boolean isEmbeddedInXML)
•    BagType,
Constructor: BagType(String role, String propertyRef, boolean isEmbeddedInXML)
•    CustomCollectionType, A custom type for mapping user-written classes that implement PersistentCollection
Constructor: CustomCollectionType(Class userTypeClass, String role, String foreignKeyPropertyName, boolean isEmbeddedInXML)
•    IdentifierBagType,
Constructor: IdentifierBagType(String role, String propertyRef, boolean isEmbeddedInXML)
•    ListType,
Constructor: ListType(String role, String propertyRef, boolean isEmbeddedInXML)
•    MapType,
Constructor: MapType(String role, String propertyRef, boolean isEmbeddedInXML)
•    SetType
Constructor: SetType(String role, String propertyRef, boolean isEmbeddedInXML)