VisualBuilder
  Home > Java > Tutorials > Writing the mapping file - Hibernate Tutorial
Tell a friend
Link to us
Total Members
      Members: 84661
     
Sitemap Forum Chat
Home
Hibernate Tutorial Home
1 . Introduction to Java Hibernate
2 . Introduction to Hibernate
3 . The Object/Relational Mapping Problem
4 . JDBC
5 . The Hibernate Alternative
6 . Hibernate Architecture and API
7 . Setting Up Hibernate
8 . Setting up Hibernate - Add the Hibernate libraries
9 . Registration Case Study
10 . Creating the Hibernate Configuration
11 . Writing the first Java File
12 . Writing the mapping file
13 . Writing the Business Component
14 . Writing the Test Client
15 . Managing Associations
16 . Finding by primary key
17 . Hibernate Query Language (HQL)
18 . Using native SQL
19 . Using Criteria Queries
20 . Using Ant to run the project
21 . Using Middlegen to generate source
22 . Review and the next steps
 
 
Java Home
Java Members (27650)
Java Member Articles ( 40 )
Java Discussion (275)
Java Q & A ( 174 )
- Java Ask Question
- Java Questions
- Java Unanswered Questions
Java Resources
Java Source Code (1096)
Java Articles (551)
Java Blogs (118)
Java Jobs (797)
Java Components (85)
Java Books (169)
Java Websites (127)
Java News (103)
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.Net Security,Internationalisation And Deployment
ASP.NET Tutorial
C# Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (451 )
Source Code (3275 )
Articles (359 )
Books (372 )
Components (1596 )
News (892 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Java hibernate Tutorial
 Writing the mapping file
  << Prev: Writing the first Java File Next: Writing the Business Component >>

Mapping files are the heart of O/R mapping tools. These are the files that contain field to field mapping between the class attributes and the database columns.
Let's write a mapping file for the User class that we want to persist to the database.


<?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="com.visualbuilder.hibernate.User" table="USERS" >
          <id name="userId" type="java.lang.Long" column="user_id"
>
                <generator class="increment"
/>
     
    </id>
          <property name="firstName" type="java.lang.String" column="first_name" length="20" />
          <property name="lastName" type="java.lang.String" column="last_name" length="20" />
          <property name="age" type="java.lang.Integer" column="age" length="-1" />
          <property name="email" type="java.lang.String" column="email" length="40" />
      </class>
 
  </hibernate-mapping>
 


As you can see,we have mapped all of the attributes of the class "User" to the columns of the table we created previously. Note the "id" attribute which maps the userId field of class User is defined in a different way than the other properties. The id tag is used to indicate that this is a primary key,and a <generator> tag is used to generate the primary key using different techniques. We have used the increment class,but there are also available different classes to support different kind of key generation techniques like generating key from a sequence,selecting from database etc. We can also use a custom key generator class.


Let us now add the mapping file to the Hibernate configuration file hibernate.cfg.xml. After adding the mapping resource entry,the hibernate.cfg.xml looks like this.


<?xml version='1.0' encoding='utf-8'?>
  <!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  <hibernate-configuration>
     <session-factory>
   
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.username">SYSTEM</property>
        <property name="connection.password">manager</property>


        <!-- Set AutoCommit to true -->
        <property name="connection.autocommit">true
</property>

        <!-- SQL Dialect to use. Dialects are database specific -->
        <property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
   
        <!-- Mapping files -->
      <mapping resource="com/visualbuilder/hibernate/User.hbm.xml" />
   
    </session-factory>
  </hibernate-configuration>


  << Prev: Writing the first Java File Next: Writing the Business Component >>
Java Hibernate Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
 
ADVERTISEMENT
Partner List
Code Project
ASP Alliance
More
 
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising