Monday, March 5, 2007

What is component mapping 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.

SOURCE : www.referjava.com

Why do you need ORM tools like hibernate?

To overcome the "paradigm mismatch" between object oriented data and table oriented relational databases.

SOURCE : www.referjava.com

What is the difference between sorted and orderd collection in hibernate?

A sorted collection is sorted in-memory using java comparator, while order collection is ordered at the database level using order by clause.

SOURCE : www.referjava.com

What is the main advantage of using the hibernate than using the sql?

Main advantage is that it avoids writing queries. Ofcourse, u have to write to some extent but a lot is relaxed.Most of the work is taken care by mapping.Also criteria class is very useful for complex joins.

Using ORM we can avoid the jdbc API completely and also provides ease to the developer in developing the classes.


SOURCE : www.referjava.com

How to create primary key using hibernate?

id field in hbm.xml file is used to specify the primary key in database. We also use generator to specify the way primary key is generated to the database. For example

< id name="testId" type="string" >
< column name="testColumn" length="40" / >
< generator class="increment" / >
< /id >

here the primary key field name in database is testColumn and it will autonmatically incremented by one as the generator is specified as increment.


SOURCE : www.referjava.com

What is the advantage of Hibernate over jdbc?

Hibernate is used to persist the objects in the database, but we have to use the jdbc to connect to database. JDBC is used to store the primitivies in the database.
Hibernate is basically a ORM tool which allows you to perform database activies without bothering about the Database change.

You dont need to change the SQL scripts if you change database.

Apart from that you dont need to write most of the SQL scripts for persisting ,deleting object and parsing the resultsets.With respect to perfomance, hibernate provide the capability to reduce the number of database trips by creating the betch processing and session cache and second level cache.

It also supports the transactions.
More then this all, it is very easy to make a cleaner seperation of Data Access Layer from usiness logic layer.
With all the capabilities mention above it is fast and easy to learn hibernate, develop application and maintain easily.


SOURCE : www.referjava.com

What is the difference between Hibernate and EJB 2.1?

Hibernate is a ORM(object relation mapping ) tool which can be used for creating a mapping between plain java bean objects (POJO) and a persitent storage (rdbms).The EJB 3.0 specification is divided into two parts The first which deals with session and MDBs and the second which deals with persistence and entity beans. The latter part is called JPA(java persistance API ). Hibernate 3.0 implements the JPA specification.EJB 2.1 is a specification for defining loosely coupled reusable business componenets.

EJB 2.1 and hibernate serve two different purposes. Hibernate can be co related with the entity beans in EJB 2.1.HIbernate offers far more extensive features then plain entity beans.still there are no containers (applicaiton servers) available which fully implement the EJB 3.0 specification. depending upon the buisness needs hibernate framework can be used in conjuction with EJB2.1 to achieve the JPA abstraction.

SOURCE : www.referjava.com

What is the difference between beans and hibernate?

Beans are at form level and hibernate at data base level.

Hibernate adapts beans.Its plain old Java objects. POJO. Hibernate always has a bean orientation.But the Bean is just not persistent, using hibernate you can make a bean persistent.A bean when used in Hibernate It can create a table ,Add a row,Delete a rowUpdate row.THis is all done with Hib Configuration stuff.Hibernate is ORM as it can relate a bean to a row in a table


SOURCE : www.referjava.com