Logo F2FInterview

Hibernate Interview Questions

Q   |   QA

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

There are three methods by which an object can be identified.

  1. Object identity –Objects are identical if they reside in the same memory location in the JVM. This can be checked by using the = = operator.
  2. Object equality – Objects are equal if they have the same value, as defined by the equals( ) method. Classes that don’t explicitly override this method inherit the implementation defined by java.lang.Object, which compares object identity.
  3. Database identity – Objects stored in a relational database are identical if they represent the same row or, equivalently, share the same table and primary key value.

XDoclet has brought the concept of attribute-oriented programming to Java. Until JDK 1.5, the Java language had no support for annotations; now XDoclet uses the Javadoc tag format (@attribute) to specify class-, field-, or method-level metadata attributes. These attributes are used to generate hibernate mapping file automatically when the application is built. This kind of programming that works on attributes is called as Attribute Oriented Programming.

Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
MyPersistanceClass mpc = new MyPersistanceClass (“Sample App”);
session.save(mpc);
tx.commit();
session.close();

The Session and Transaction are the interfaces provided by hibernate. There are many other interfaces besides this.

In order to make most of the things work in hibernate, usually the information is provided in an xml document. This document is called as xml mapping document. The document defines, among other things, how properties of the user defined persistence classes’ map to the columns of the relative tables in database.

<?xml version=”1.0″?> <!DOCTYPE hibernate-mapping PUBLIC
“http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd”>
<hibernate-mapping>
<class name=”sample.MyPersistanceClass” table=”MyPersitaceTable”>
<id name=”id” column=”MyPerId”>
<generator class=”increment”/>
</id>
<property name=”text” column=”Persistance_message”/>
<many-to-one name=”nxtPer” cascade=”all” column=”NxtPerId”/>
</class>
</hibernate-mapping>

Everything should be included under <hibernate-mapping> tag. This is the main tag for an xml mapping document.

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: