Hibernate Coding
Hibernate Coding
Simple Example
<?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">
<session-factory>
<property name="connection.username">username</property>
<property name="connection.url">
jdbc:oracle:thin:username/password@localhost:1521/XE
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect </property>
<property
name="myeclipse.connection.profile">MyDriver</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver</property>
Simple.hbm.xml :
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Simple" table="SIMPLE" schema="USERNAME">
<id name="id" type="java.lang.Long">
<column name="ID" precision="2" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="21" />
</property>
</class>
</hibernate-mapping>
Client class
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.transaction.*;
public class mainSimple {
public mainSimple() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sf = new Configuration().configure().
buildSessionFactory();
Session ses = sf.openSession();
Transaction tx = ses.beginTransaction();
Simple sob=new Simple();
Sob.setId(21);
Sob.setName(“LCBK Raju”);
Ses.save(sob);
Tx.close();
Ses.close();
}
}
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION =
"/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new
ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory =
configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating
SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory =
configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating
SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory()
{
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
b. Get Session Factory
Session ses=HibernateSessionfactory.getSession();
ManyToOne Bidirectional:
public class Item {
...
private Set bids = new HashSet();
public void setBids(Set bids) {
this.bids = bids;
}
public Set getBids() {
return bids;
}
public void addBid(Bid bid) {
bid.setItem(this);
bids.add(bid);
}
...
}