Uncategorized


This is continuation of my previous post.

We discussed with different type of generators for primary keys and we have seen an example with ‘assigned’ previously. We can see ‘generator‘ in this post.

RoseIndia says we shouldnt use this in clustered invironment.

Create a table using the following structure.

BOOK-table structure

BOOK-table structure

Let your DTO be like this

package hib;

public class Book {
 private long bookID;
 private String bookName;
 public long getBookID() {
 return bookID;
 }
 public void setBookID(long bookID) {
 this.bookID = bookID;
 }
 public String getBookName() {
 return bookName;
 }
 public void setBookName(String bookName) {
 this.bookName = bookName;
 }
}

Add the following entries to contact.hbm.xml inside the root element

<class name="hib.Book" table="BOOK">
 <id name="bookID" type="long" column="ID">
 <generator></generator>
 </id>
 <property name="bookName">
 <column name="BOOKNAME"></column>
 </property>
 </class>

Let your client program be as follows

package hib;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class IncrementTest {

 /**
 * @param args
 */
 public static void main(String[] args) {
 Session session=null;
 try {
 SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
 session = sessionFactory.openSession();
 Transaction tx = session.beginTransaction();
 System.out.println("Transaction beginning");
 Book book = new Book();
 book.setBookName("LIVE LA FRANCE");
 session.save(book);
 System.out.println("book stored");
 tx.commit();
 } catch (HibernateException e) {
 e.printStackTrace();
 }finally
 {
 if (session!=null){
 session.flush();
 session.close();
 }
 }
 }
}

And be happy, your inserted the records with auto incrementing ID :)

book console

book console

book result

book result

This is in continuation of my previous post.

Lets take the hibernate mapping file and get to the tags used in that file.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
 <class name="hib.Contact" table="CONTACT">
 <id name="id" type="long" column="ID" >
 <generator/>
 </id>

 <property name="firstName">
 <column name="FIRSTNAME" />
 </property>
 <property name="lastName">
 <column name="LASTNAME"/>
 </property>
 <property name="email">
 <column name="EMAIL"/>
 </property>
 </class>
</hibernate-mapping>

We could see some XML tags tries forms this mapping. Their explanations are given below.

hibernate-mapping

Root element of this mapping

class

maps the java class and SQL table

id

This will handle the primary key constraint. In addition this is the unique representation of an object.
Attributes of ID elements are given below.

name:

The property name used by the persistent class.

column:

The column used to store the primary key value.

type:

The Java data type used.

unsaved-value:

This is the value used to determine if a class has been made persistent. If the value of the id attribute is null, then it means that this object has not been persisted.

generator

This is the notification to generate the primary key. The following values to this attribute decides the type of generator

Increment:

generate primary keys of type long, short or int that are unique only. It should not be used in the clustered deployment environment.

Sequence:

Hibernate can also use the sequences to generate the primary key. It can be used with DB2, PostgreSQL, Oracle, SAP DB databases.

Assigned

Assigned method is used when application code generates the primary key.

property

The property elements define standard Java attributes and their mapping into database schema. The property element supports the column child element to specify additional properties, such as the index name on a column or a specific column type

hai, friends it’s test posting for wordpress 2.1 to 2.7 upgrading…. :)

Next Page »