Tell a friend
Link to us
Bookmark Us
Total Members
      Members: 87081
Sitemap Forum Chat
 
Java Home
Java Members (28736)
Java Member Articles ( 41 )
Java Discussion (283)
Java Q & A ( 172 )
- Java Ask Question
- Java Questions
- Java Unanswered Questions
Java Resources
Java Source Code (1096)
Java Articles (552)
Java Blogs (133)
Java Jobs (801)
Java Components (85)
Java Books (169)
Java Websites (127)
Java News (103)

 
Resource Directory
Java Hosting (0)
Source Code (0)

 
GROUP INFO
Members: 28736
Access Type: Anyone can join

 
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 Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
LINQ Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Javascript Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Grails Tutorial
Hibernate Tutorial
Java 1.6 Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Service-Oriented Architecture (SOA) Tutorial For Managers
Spring Tutorial
Struts Tutorial
Tomcat Tutorial

RESOURCES
Q & A (531 )
Source Code (3276 )
Articles (365 )
Books (373 )
Components (1647 )
News (898 )
Websites (1208 )

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

COMMUNITY
Authors
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Feedback
Tell a Friend
Advertise

 

Home > Java > Member Articles > Database >
 

Hibernate Tutorial


  Author : VisualBuilder.com
  Date Published : 05/13/2007
  Accessed : 21270
   visualbuilder
<< Back  Next >> 

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>


Pages   << Back  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  Next >>  Next: Part 13: Writing the Business Component >>

 
   Printer Friendly
   Email to a friend
   Add to my Favourites  Report Bad Submissions  Submit Feedback
  Download PDF version
 
                 Click each image to add
this page to each site.
 
Related Articles of Java
Previous Article    -     Random Articles    -     Next Article
 
 
 
 
 
 
 
 
Welcome Guest Signup
Member's Panel
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!

SEARCH
 



 
 
 
 
 


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

Warning: Unknown(): write failed: Disk quota exceeded (122) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0