Hibernate one to many XML - mapping:
EX -7:
Dept.java
package madhav;
import
java.util.HashSet;
import java.util.Set;
public class Dept {
private int deptno;
private String dname;
private String loc;
private
Set<Employee> employees=new HashSet<Employee>(0);
public int getDeptno() {
return deptno;
}
public Set<Employee>
getEmployees() {
return employees;
}
public void
setEmployees(Set<Employee> employees) {
this.employees = employees;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String
getDname() {
return dname;
}
public void setDname(String
dname) {
this.dname = dname;
}
public String getLoc()
{
return loc;
}
public void setLoc(String
loc) {
this.loc = loc;
}
}
Employee.java
package madhav;
public class Employee {
private int empno;
private String ename;
private Dept dept;
public Dept getDept()
{
return dept;
}
public void setDept(Dept
dept) {
this.dept = dept;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String
getEname() {
return ename;
}
public void setEname(String
ename) {
this.ename = ename;
}
}
dept.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="madhav">
<class name="Dept"
table="dept">
<id name="deptno"
type="int" column="deptno"
>
<generator class="increment"/>
</id>
<property name="dname">
<column name="dname"
/>
</property>
<property name="loc">
<column name="loc"/>
</property>
<set name="employees"
table="employee" inverse="true"
cascade="all">
<key >
<column name="deptno"
not-null="true"/>
</key>
<one-to-many class="Employee"
/>
</set>
</class>
</hibernate-mapping>
Employee.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="madhav">
<class name="Employee"
table="employee">
<id name="empno"
type="int">
<column name="empno"></column>
<generator class="increment"/>
</id>
<property name="ename">
<column name="ename"
/>
</property>
<many-to-one name="dept"
class="Dept" fetch="select">
<column name="deptno"
not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>
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">
<!--
Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="connection.username">madhav</property>
<property name="connection.password">madhav</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="employee.hbm.xml"/>
<mapping resource="dept.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TestSupport.java
import madhav.*;
import org.hibernate.SessionFactory;
import
org.hibernate.classic.Session;
import
org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestSupport {
/**
* @param args
*/
public void insert()
{
System.out.println("execution
started");
TestCase t=new
TestCase();
Configuration cfg=new
Configuration();
cfg.configure();
//cfg.addResource("employee.hbm.xml");
//cfg.addResource("personaldetails.hbm.xml");
SessionFactory
factory=cfg.buildSessionFactory();
Session
session=null;
Transaction
tx=null;
try
{
session =factory.openSession();
tx=session.beginTransaction();
Dept dept = new Dept();
dept.setDname("hugo");
dept.setLoc("hyd");
session.save(dept);
Employee emp1 = new Employee();
emp1.setEname("madhav");
emp1.setDept(dept);
Employee emp2 = new Employee();
emp2.setEname("narendar");
emp2.setDept(dept);
session.save(emp1);
session.save(emp2);
tx.commit();
System.out.println("record
inserted");
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
}
TestCase.java
import madhav.*;
import
org.hibernate.SessionFactory;
import
org.hibernate.classic.Session;
import
org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestCase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestSupport ts=new
TestSupport();
ts.insert();
}
}
Output-console:
Related Topics:
- hibernate-basic-example(xml-based)
- hibernate-inheritance-tableforclass-hierarchy(xml-based)
- hibernate-inheritance-joinsubclass-(xml-based)
- hibernate-inheritance-tableforconcreteclass(xml-based)
- hibernate-onetoone-relation(xml-based)
- hibernate-onetomany-relation(xml-based)
- hibernate-manytomany-relation(xml-based)
- hibernate-basic-example(Annotation-based)
- hibernate-inheritance-tableforclass-hierarchy(Annotation-based)
- hibernate-inberitance-joinsubclass-(Annotation-based)
- hibernate-inheritance-tableforconcreteclass(Annotation-based)
- hibernate-onetoone-relation(Annotation-based)
- hibernate-onetomany-relation(Annotation-based)
- hibernate-manytomany-relation(Annotation-based)
No comments:
Post a Comment