Mapping List Object in Hibernate


Below self explanatory sample application helps to understand how to persist a list object using Hibernate as persistence layer and MySQL as database.

—————————————————————————————————

package project2;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.Transaction;

import project2.persistence.HibernateUtil;
public class MappingListDemo {

public static void main(String[] args) {

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();

try {
customers cust1 = new customers(“Dave”);

ArrayList list1 = new ArrayList();
list1.add(new custorders(“SOA Using Java Web Services”));
list1.add(new custorders(“Java Persistence with Hibernate”));
cust1.setOrders(list1);
session.save(cust1);

customers cust2 = new customers(“Jim”);
ArrayList list2 = new ArrayList();
list2.add(new custorders(“Java Unleashed”));
list2.add(new custorders(“Design Patterns”));
cust2.setOrders(list2);
session.save(cust2);

tx.commit();

session.flush();
session.close();
} catch (Exception he)

{
System.out.println(“Exception caught: ” + he);
}
}

}

————————————————————————————————–

————————————————————————————————–

package project2;
public class custorders {
String orderinfo;

int id;
public custorders() {
}
public custorders(String d) {
orderinfo = d;
}
public int getId() {
return id;
}

public void setId(int s) {
id = s;
}
public void setOrderinfo(String orderinfo) {
this.orderinfo = orderinfo;
}

public String getOrderinfo() {
return orderinfo;
}
}

————————————————————————————————–

—————————————————————————————————

package project2;
import java.util.List;

public class customers {
String name;
List orders;
int id;

public customers() {
}

public customers(String d) {
name = d;
}
public String getName() {
return name;
}

public void setName(String a) {
name = a;
}
public int getId() {
return id;
}

public void setId(int s) {
id = s;
}
public void setOrders(List orders) {
this.orders = orders;
}

public List getOrders() {
return orders;
}
}

————————————————————————————————–

—————————————————————————————————

<?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=”project2.customers”   table=”customer”>
<id name=”id” >

<generator/>

</id>

<property name=”name” />

<list name=”orders” cascade=”all”>

<key column=”item_id” />
<list-index column=”position” />
<one-to-many />

</list>

</class>

</hibernate-mapping>

————————————————————————————————–

—————————————————————————————————

<?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=”project2.custorders”   table=”custorder”>
<id name=”id”>

<generator/>

</id>

<property name=”orderinfo”  />

</class>

</hibernate-mapping>

————————————————————————————————–

—————————————————————————————————
<!DOCTYPE hibernate-configuration SYSTEM
http://hibernate.sourceforge.net/hibernate-configuration-3.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:3306/hibernatetest</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.connection.password”></property>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
<!– Use the C3P0 connection pool provider –>
<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>
<!– Show and print nice SQL on stdout –>
<property name=”show_sql”>true</property>
<property name=”format_sql”>true</property>
<!– List of XML mapping files –>

<mapping/>
<mapping/>

<mapping resource=”project2/custorders.hbm.xml”/>
<mapping resource=”project2/customers.hbm.xml”/>
</session-factory>
</hibernate-configuration>

—————————————————————————————————

MySQL query tables used in above examples…

create table custorder(id int not null primary key auto_increment,
orderinfo text,position int,item_id int);

create table customer
( id int not null primary key auto_increment, name VARCHAR(40));

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: