Hibernate Collection Mapping example
hibernate.cfg.xml
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">minu</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/Mapping/hibernate/hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.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" >
<hibernate-mapping package="com.Mapping.hibernate">
<class name="Student" table="student">
<id name="sid" column="sid" type="int">
<generator class="increment"/>
</id>
<property name="sname" column="sname" type="string"/>
<property name="dob" column="dob" type="string"/>
<property name="qualification" column="quali" type="string"/>
<array name="courses" table="courses">
<key column="sid"/>
<index column="idx"/>
<element column="cname" type="string"/>
</array>
<list name="emails" table="emails">
<key column="sid"/>
<index column="idx"/>
<element column="emails" type="string"/>
</list>
<bag name="marks" table="marks">
<key column="sid"/>
<element column="marks" type="int"/>
</bag>
<set name="phones" table="phones">
<key column="sid"/>
<element column="phones" type="long"/>
</set>
<map name="refs" table="refs">
<key column="sid"/>
<index column="rname" type="string"/>
<element column="rphone" type="long"/>
</map>
</class>
</hibernate-mapping>
Student.java
package com.Mapping.hibernate;
import java.util.*;
public class Student {
private int sid;
private String sname;
private String dob;
private String qualification;
private String[] courses;
private List<String> emails;
private List<Integer> marks;
private Set<Long> phones;
private Map<String,Long> refs;
public Student(){}
public Student(int sid, String sname, String dob, String qualification,
String[] courses, List<String> emails, List<Integer> marks,
Set<Long> phones, Map<String, Long> refs) {
super();
this.sid = sid;
this.sname = sname;
this.dob = dob;
this.qualification = qualification;
this.courses = courses;
this.emails = emails;
this.marks = marks;
this.phones = phones;
this.refs = refs;
}
public Student(String sname, String dob, String qualification,
String[] courses, List<String> emails, List<Integer> marks,
Set<Long> phones, Map<String, Long> refs) {
super();
this.sname = sname;
this.dob = dob;
this.qualification = qualification;
this.courses = courses;
this.emails = emails;
this.marks = marks;
this.phones = phones;
this.refs = refs;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public String[] getCourses() {
return courses;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public List<String> getEmails() {
return emails;
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
public List<Integer> getMarks() {
return marks;
}
public void setMarks(List<Integer> marks) {
this.marks = marks;
}
public Set<Long> getPhones() {
return phones;
}
public void setPhones(Set<Long> phones) {
this.phones = phones;
}
public Map<String, Long> getRefs() {
return refs;
}
public void setRefs(Map<String, Long> refs) {
this.refs = refs;
}
}
HibernateUtil.java
package com.Mapping.hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
static SessionFactory factory;
static
{
try
{
Configuration cfg=new Configuration();
cfg=cfg.configure();
factory=cfg.buildSessionFactory();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory()
{
return factory;
}
}
Lab.java
package com.Mapping.hibernate;
import java.util.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class Lab {
public static void main(String[] args) {
try
{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
String cou[]={"JDBC","JSF","J2EE","EJB"};
List<String> ems=new ArrayList<String>();
ems.add("a1@gmail.com");
ems.add("a2@gmail.com");
ems.add("a3@gmail.com");
List<Integer> mks=new ArrayList<Integer>();
mks.add(new Integer(100));
mks.add(new Integer(89));
mks.add(new Integer(90));
Set<Long> phs=new HashSet<Long>();
phs.add(new Long(111));
phs.add(new Long(222));
phs.add(new Long(333));
Map<String,Long> refs=new HashMap<String,Long>();
refs.put("aaa",new Long(122345));
refs.put("bbb",new Long(129856));
refs.put("ccc",new Long(129978));
Student st=new Student("Minu","11-08-1988","BTech",cou,ems,mks,phs,refs);
session.save(st);
tx.commit();
session.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Comments
Post a Comment