1. Create a new class Message as such
package com.testHiber;
public class Message {
private Long id;
private String text;
private Message nextMessage;
Message() {
}
public Message(String text) {
this.text = text;
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}
2. Create a file Message.hbm.xml under the same package.
< ?xml version="1.0"? >
< !DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
< class name="com.testHiber.Message" table="MESSAGES" >
< id name="id" column="MESSAGE_ID" >
< generator class="increment"/ >
< /id >
< property name="text" column="MESSAGE_TEXT"/ >
< many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID" foreign-key="FK_NEXT_MESSAGE"/ >
< /class >
< /hibernate-mapping >
Note: There should be a table in the database.
3. Create the main class here as such then u can run the program
package com.testHiber;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import persistence.*;
public class HelloWorld {
public static void main(String[] args) {
try{
// First unit of work
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello World");
Long msgId = (Long) session.save(message);
tx.commit();
session.close();
// Second unit of work
Session newSession = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = newSession.beginTransaction();
List messages = newSession.createQuery("from Message m order by m.text asc").list();
System.out.println( messages.size() + " message(s) found:");
for (Iterator iter = messages.iterator();
iter.hasNext();) {
Message loadedMsg = (Message) iter.next();
System.out.println(loadedMsg.getText());
}
newTransaction.commit();
newSession.close();
// Shutting down the application
HibernateUtil.shutdown();
}
catch(Exception e){
System.out.println(" Exception in my code "+e.getLocalizedMessage());
e.printStackTrace();
}
}
}
4. Specify the hibernate.cfg.xml the hibernate configuration file as such...
< !DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
< hibernate-configuration >
< session-factory >
< property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver< /property >
< property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:/userDB< /property >
< property name="hibernate.connection.username">sa< /property >
< property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect< /property >
< property name="hibernate.c3p0.min_size">5< /property >
< property name="hibernate.c3p0.max_size">20< /property >
< property name="hibernate.c3p0.timeout">300< /property >
< property name="hibernate.c3p0.max_statements">50< /property >
< property name="hibernate.c3p0.idle_test_period">3000< /property >
< property name="show_sql">true
< property name="format_sql">true< /property >
< mapping resource="com/testHiber/Message.hbm.xml" />
< /session-factory >
< /hibernate-configuration >
Note:
This specifies the database and table needs to be mapped.. File has to be placed under the root directory of the package.
The file Message.hbm.xml has to be mapped here in this hibernate.cfg.xml.
6. Build all files using the build.xml 1and you can get the desired output.
Tuesday, June 2, 2009
Subscribe to:
Post Comments (Atom)
Good to follow and easy to understand... Thanks
ReplyDelete