One to Many : Uni-Directional Relation in Hibernate
There are two table WRITER and STORY. One writer can write multiple stories. So the relation is one-to-many uni-directional.
Step 1. create a table name WRITER and STORY in your database
create table WRITER (
ID number, //PRIMARY KEY
NAME varchar
);
create table STORY (
ID number,
INFO varchar,
PARENT_ID number // forign key - relate to ID Coulmn of WRITER.
);
Step 2. Mapping writer.hbm.xml- Which maps to WRITER TABLE.
public class Writer {
private int id;
private String name;
private List stories;
public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setStories(List l) {
stories = l;
}
public List getStories() {
return stories;
}
}
Step 5. Create Story.java bean class.
public class Story {
private int id;
private String info;
public Story(){
}
public Story(String info) {
this.info = info;
}
public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public void setInfo(String n) {
info = n;
}
public String getInfo() {
return info;
}
}
Step 6. add writer.hbm.xml and story.hbm.xml into hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<lt;!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/techfaqdb</property>
<property name="connection.username">techfaq</property>
<property name="connection.password">techfaq</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">4</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<!-- MySQL dialect//different for different Database -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="story.hbm.xml"/>
<mapping resource="writer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Here is the code : how to save
Save Example ..
Writer wr = new Writer();
wr.setName("Das");
ArrayList list = new ArrayList();
list.add(new Story("Story Name 1"));
list.add(new Story("Story Name 2"));
wr.setStories(list);
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(wr);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
session.close();
}