Thursday, 16 February 2012

What is Hibernate proxy? | Java Hibernate

Mapping of classes can be made into a proxy instead of a table. A proxy is returned when actually a load is called on a session. The proxy contains actual method to load the data. The proxy is created by default by Hibernate, for mapping a class to a file. The code to invoke Jdbc is contained in this class.
Explain the types of Hibernate instance states. 
The persistent class’s instance can be in any one of the three different states. These states are defined with a persistence context. The Hibernate has the following instance states:
•    Transient: This instance is never been associated with any one of the persistence process. This does not have persistent identity like primary key value.
•    Persistent: A persistent context is made to associate with the current instance. It has persistent identity like primary key value and a corresponding row of a table in the data base. Hibernate guarantees the persistent identity is equivalent to the java Identity [object], for a particular persistence context
•    Detatched: This instance association with a persistence context is only once and the context was closed or serialized to another process. The persistent identity is retained and it can be a corresponding row in a database.

What are the benefits of HibernateTemplate? Java Hibernate

The benefits of HibernateTemplate are:
•    HibernateTemplate, which is a Spring Template class, can simplify the interactions with Hibernate Sessions.
•    Various common functions are simplified into single method invocations.
•    The sessions of hibernate are closed automatically
•    The exceptions will be caught automatically, and converts them into runtime exceptions.

What is a HibernateTemplate? | Java Hibernate

HibernateTemplate is a helper class that is used to simplify the data access code. This class supports automatically converts HibernateExceptions which is a checked exception into DataAccessExceptions which is an unchecked exception. HibernateTemplate is typically used to implement data access or business logic services. The central method is execute(), that supports the Hibernate code that implements HibernateCallback interface.

Explain how to configure Hibernate? | Java Hibernate

Answer : 1
Hibernate uses a file by name hibernate.cfg.xml. This file creates the connection pool and establishes the required environment. A file named .hbm.xml is used to author mappings. The bootstrap action is configured by using Configuration interface.
There are two types of environment to configure hibernate:
1. Managed Environment: The definitions of database operations such as connections, transaction boundaries, security levels. This environment is provided by application servers such as Jboss,Weblogic,Websphere.
2. Non-managed Environment: The basic configuration template is provided by this interface. Tomcat is one of the examples that best supports this environment.
Answer :2
Programmatic configuration
The org.hibernate.cfg.Configuration instance can be instantiated directly by specifying XML mapping documents. If the mapping files are in the classpath, use addResource().
Configuration cfg = new Configuration()
.addResource("Item.hbm.xml")
.addResource("Bid.hbm.xml");
An alternative way is to specify the mapped class and allow Hibernate to find the mapping document:
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class);
org.hibernate.cfg.Configuration also allows you to specify configuration properties:
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
.setProperty("hibernate.order_updates", "true");
Alternative options include:
Passing an instance of java.util.Properties to Configuration.setProperties().
Placing a file named hibernate.properties in a root directory of the classpath.
Setting System properties using java -Dproperty=value.
Including <property> elements in hibernate.cfg.xml (this is discussed later).
Hibernate JDBC Properties:
hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.connection.pool_size

What are the main advantages of ORM like hibernate? | Java Hibernate

Hibernate implements extremely high-concurrency architecture with no resource-contention issues. This architecture scales extremely well as concurrency increases in a cluster or on a single machine.
Other performance related optimizations that hibernate performs are:
•    Caching objects
•    Executing SQL statements later, when needed
•    Never updating unmodified objects
•    Efficient Collection Handling
•    Rolling two updates into one
•    Updating only the modified columns
•    Outer join fetching
•    Lazy collection initialization
•    Lazy object initialization

Why do you need ORM tool like Hibernate? | Java Hibernate

ORM tools like hibernate provide following benefits:
•    Improved performance: Lazy loading, Sophisticated caching, Eager loading
•    Improved productivity: High-level object-oriented API, Less Java code to write, No SQL to write
•    Improved maintainability: A lot less code to write
•    Improved portability: ORM framework generates database-specific SQL for you

What are managed associations and hibernate associations? | Java Hibernate

Associations that are related to container management persistence are called managed associations. These are bi-directional associations. Coming to hibernate associations, these are unidirectional.