Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Understanding Hibernate O/R Mapping 
 

In the last example we created contact.hbm.xml to map Contact Object to the Contact table in the database. Now let's understand the each component of the mapping file.

 

Understanding Hibernate O/R Mapping

                         

In the last example we created contact.hbm.xml to map Contact Object to the Contact table in the database. Now let's understand the each component of the mapping file.

 

 

 

 

To recall here is the content of contact.hbm.xml:

<?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="roseindia.tutorial.hibernate.Contact" table="CONTACT">
   <id name="id" type="long" column="ID" >
   <generator class="assigned"/>
  </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>

 

Hibernate mapping documents are simple xml documents. Here are important elements of the mapping file:.

  1. <hibernate-mapping> element
    The first or root element of hibernate mapping document is <hibernate-mapping> element. Between the <hibernate-mapping> tag class element(s) are present.
       
  2.  <class> element
    The <Class> element maps the class object with corresponding entity in the database. It also tells what table in the database has to access and what column in that table it should use. Within one <hibernate-mapping> element, several <class> mappings are possible.
      
  3.  <id> element
    The <id> element in unique identifier to identify and object. In fact <id> element map with the primary key of the table. In our code :
    <id name="id" type="long" column="ID" >
    primary key maps to the ID field of the table CONTACT. The attributes of the id element are:
    • 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.
          
  4. <generator> element
    The <generator> method is used to generate the primary key for the new record. Here is some of the commonly used generators :
       
    * Increment - This is used to 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. 
       
       
  5. <property> element
    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.

                             

» View all related tutorials
Related Tags: sql c hibernate api com class ant comparison import io property method sed const operators expression operator port criteria name

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

47 comments so far (
post your own) View All Comments Latest 10 Comments:

hi,
i dint get solution for one to many still.
I tried out the above given code too.
can u give me the proper steps or site for proper
tutorial to be followed?

Posted by shanmuganathan on Monday, 10.13.08 @ 10:01am | #81036

hi team,
we did one to one hibernate mapping.
we are trying one to many mapping.But its difficult for us. Can u help us? And can anyone provide good tutorial for solving this??Thanks in advance

Posted by shanmuganathan on Wednesday, 10.8.08 @ 12:48pm | #80960

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set

System.out.println("Inserting Record");
Contact contact = new Contact();
//contact.setId(1);
contact.setFirstName("Nitin");
contact.setLastName("Kumar");
contact.setEmail("nitin_38@yahoo.com");
Transaction tx = session.beginTransaction();
session.save(contact);
tx.commit();
System.out.println("Done");

Posted by Nitin Gupta on Tuesday, 09.30.08 @ 11:40am | #80793

Hi
Basically i am newly reader for Hibernate.How can run a simple Hibernate Web Application on NetBean5.5 IDE.How i use Jdbc with Hibernate.

sanjeev

Posted by Sanjeev on Thursday, 09.11.08 @ 14:27pm | #78666

Very easy to understand for the beginners but, I request the roseindia team to publish it with out any errors and list out the jars and their usage in the application.

Posted by LaxmiKanth on Tuesday, 09.9.08 @ 12:54pm | #78224

It's the same question that someone has asked before but I don't see the answer being posted. I tried the example code, the schema gets created, but data is never inserted into the table Contact.

What to look and how to get it working?

Thanks.

Posted by Smitha on Wednesday, 08.27.08 @ 00:49am | #75347

Thanks,

This is a very easy and good example for Hibernate beginers.

Posted by Hemant Chitte on Monday, 06.2.08 @ 17:49pm | #61827


Can u please send me some document of hibernate.

Posted by swadhin on Saturday, 04.26.08 @ 15:08pm | #57927

Hi,

your explanation and example is too easy and too great for a newly reader.

Posted by swadhin on Saturday, 04.26.08 @ 15:06pm | #57926


problem parsing :configuration/hibernate.cfg.xml

ANy onr can tell me why this error id appera when i m rum my application

Posted by Amit Gupta on Wednesday, 12.12.07 @ 11:55am | #41915

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.