-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: help me
PostPosted: Wed Apr 27, 2005 5:45 am 
Newbie

Joined: Wed Apr 27, 2005 5:03 am
Posts: 10
Cmon all u masters os hibernate, giv me the creect steps to be followed to implement hibernate in struts application in ur own words.starting from configuring the xml file


Top
 Profile  
 
 Post subject: Building Hibernate Application
PostPosted: Fri Apr 29, 2005 5:30 am 
Newbie

Joined: Fri Apr 29, 2005 4:58 am
Posts: 12
Location: Chennai INDIA
1.Hibernate uses the hibernate.cfg.xml to create the connection pool and setup required environment

--------------------------------
hibernate.cfg.xml to create the connection pool and setup required environment. Here is the code:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

------------------------------------------

2. Writing First Persistence Class
Hibernate uses the Plain Old Java Objects (POJOs) classes to map to the database table. We can configure the variables to map to the database column. Here is the code for Contact.java:

Writing First Persistence Class
Hibernate uses the Plain Old Java Objects classes to map to the database table. We can configure the variables to map to the database column. Here is the code for Contact.java:

package waltair.tutorial.hibernate;

/**
* @author Thomas M. Waltair
*
* Java Class to map to the datbase Contact Table
*
*/
public class Contact {
private String firstName;
private String workingCompany;
private String personalEmail;
private long id;

/**
* @return
*/
public String getFirstName() {
return firstName;
}

/**
* @return
*/
public long getId() {
return id;
}

/**
* @return
*/
public String getPersonalEmail() {
return personalEmail;
}

/**
* @return
*/
public String getWorkingCompany() {
return workingCompany;
}

/**
* @param string
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param l
*/
public void setId(long l) {
id = l;
}

/**
* @param string
*/
public void setPersonalEmail(String string) {
personalEmail = string;
}

/**
* @param string
*/
public void setWorkingCompany(String string) {
workingCompany = string;
}

}




------------------------------------------
Mapping the Contact Object to the Database Contact table
The file contact.hbm.xml is used to map Contact Object to the Contact table in the database. Here is the code for contact.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.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="workingCompany">
<column name="WORKINGCOMPANY"/>
</property>
<property name="personalEmail">
<column name="PERSONALEMAIL"/>
</property>
</class>
</hibernate-mapping>

------------------------------------------

4. Here is the code of FirstExample.java

package waltair.tutorial.hibernate;

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


/**
* @author Thomas M.Waltair
* thomaswaltair@hotmail.com
*
* An Hibernate example to INSERT data into Contact table
*
**/

public class InsertDateExample {
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();

//Create new instance of Contact and set values in it by reading them from form object

System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("ABC");
contact.setWorkingCompany("XYZ");
contact.setPersonalEmail("thomaswaltair@hotmail.com");
session.save(contact);
System.out.println("Done");

}catch(Exception e){
System.out.println(e.getMessage());
}finally{

// Actual contact insertion will happen at this step
session.flush();
session.close();
}
}
}

----------------------------------------------------

Copy contact.hbm.xml, and hibernate.cfg.xml in the bin directory of the project . Then run this example

This will run the Hibernate example program following output will displayed


----------
insert into CONTACT (FIRSTNAME, WORKINGCOMPANY, PERSONALEMAIL, ID) values(?,?,??)
----------

----------------------------------------------------

All the very Best


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.