Hibernate一对多的数据关联
2009年03月02日 评论(0)|浏览(29) 点击查看原文
(2)一对多的关联(重点)
一个部门有多个雇员:
.部门表
.雇员表
create table dept
(
deptno int identity(1,1) primary key,
dname varchar(20) not null,
loc varchar(20) not null
)
create table emp
(
empno int identity(1,1) primary key,
deptno int,
ename varchar(20) not null,
saly float not null,
hirdate datetime,
foreign key(deptno) references dept(deptno)
)
建立POJO类之间的关系
Dept.java:
import java.util.*;
public class Dept {
private int id;
private String dname;
private String loc;
//一对多集合,与此Dept类对应的所有的Emp类全部放在Set集合中
private Set emp=new HashSet(0);
public Set getEmp() {
return emp;
}
public void setEmp(Set emp) {
this.emp = emp;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
Emp.java:
import java.util.Date;
public class Emp {
private int id;
private String ename;
private float saly;
private Date hirdate;
//dept表示此Emp类关联的Dept类
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Date getHirdate() {
return hirdate;
}
public void setHirdate(Date hirdate) {
this.hirdate = hirdate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public float getSaly() {
return saly;
}
public void setSaly(float saly) {
this.saly = saly;
}
}
配置映射文件:
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>
<class name="emp.Dept" table="dept" catalog="myshop" schema="dbo">
<id name="id" type="java.lang.Integer">
<column name="deptno"></column>
<generator class="native"></generator>
</id>
<property name="dname" type="java.lang.String">
<column name="dname" length="20" not-null="true"/>
</property>
<property name="loc" type="java.lang.String">
<column name="loc" length="20" not-null="true"></column>
</property>
<!-- 配置set集合,name表示Dept类中的属性名称 -->
<!-- inverse 表示Emp类是否有主动权,为true表示没有主动权 -->
<!-- cascade 表示两个表的关联性 -->
<!-- lazy 表示是否延迟加载,为true表示延迟加载 -->
<set name="emp" inverse="true" cascade="all" lazy="true">
<key column="deptno" not-null="true"></key>
<one-to-many class="emp.Emp"/>
</set>
</class>
</hibernate-mapping>
Emp.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>
<class name="emp.Emp" table="emp" catalog="myshop" schema="dbo">
<id name="id" type="java.lang.Integer">
<column name="empno"></column>
<generator class="native"></generator>
</id>
<property name="ename" type="java.lang.String">
<column name="ename" length="20" not-null="true"></column>
</property>
<property name="saly" type="java.lang.Float">
<column name="saly" not-null="true"/>
</property>
<property name="hirdate" type="java.util.Date">
<column name="hirdate" not-null="false"></column>
</property>
<many-to-one name="dept" class="emp.Dept" fetch="select">
<column name="deptno" not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>
测试类:
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class Test {
private SessionFactory sessionFactory;
private Session session;
public Test()
{
sessionFactory=new Configuration().configure().buildSessionFactory();
session=sessionFactory.openSession();
}
public void doCreate()
{
Dept p=new Dept();
p.setDname("销售部");
p.setLoc("很好");
//创建两个与Dept相关联的类e,e1
Emp e=new Emp();
e.setEname("张三");
e.setSaly(1000.0f);
e.setHirdate(new Date());
Emp e2=new Emp();
e2.setEname("李四");
e2.setSaly(2000.0f);
e2.setHirdate(new Date());
//把两个Emp 对象添加到Set集合中
p.getEmp().add(e);
p.getEmp().add(e2);
//Emp类也要设置关联
e.setDept(p);
e2.setDept(p);
//保存Dept类同时,也会保存与Dept相关联的Emp类e,e1
session.save(p);
session.beginTransaction().commit();
session.close();
}
public void find2()
{
Dept d=(Dept)session.get(Dept.class, new Integer(1));
System.out.println(d.getDname());
System.out.println(d.getLoc());
}
public void find1()
{
String hql="FROM Dept d";
Query q=session.createQuery(hql);
Dept d=(Dept)q.uniqueResult();
Set<Emp> emp=d.getEmp();
Iterator<Emp> it=emp.iterator();
while(it.hasNext())
{
Emp e=it.next();
System.out.println(e.getEname());
System.out.println(e.getHirdate());
System.out.println(e.getSaly());
}
}
public void find3()
{
String hql="FROM Emp e WHERE e.dept.id=1";
Query q=session.createQuery(hql);
List emp=q.list();
for(int i=0;i<emp.size();i++)
{
Emp e=(Emp)emp.get(i);
System.out.println(e.getEname()+" "+e.getSaly());
}
}
public static void main(String[] args) {
//new Test().doCreate();
new Test().find1();
}
}
2009年03月02日 评论(0)|浏览(29) 点击查看原文
(2)一对多的关联(重点)
一个部门有多个雇员:
.部门表
.雇员表
create table dept
(
deptno int identity(1,1) primary key,
dname varchar(20) not null,
loc varchar(20) not null
)
create table emp
(
empno int identity(1,1) primary key,
deptno int,
ename varchar(20) not null,
saly float not null,
hirdate datetime,
foreign key(deptno) references dept(deptno)
)
建立POJO类之间的关系
Dept.java:
import java.util.*;
public class Dept {
private int id;
private String dname;
private String loc;
//一对多集合,与此Dept类对应的所有的Emp类全部放在Set集合中
private Set emp=new HashSet(0);
public Set getEmp() {
return emp;
}
public void setEmp(Set emp) {
this.emp = emp;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
Emp.java:
import java.util.Date;
public class Emp {
private int id;
private String ename;
private float saly;
private Date hirdate;
//dept表示此Emp类关联的Dept类
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Date getHirdate() {
return hirdate;
}
public void setHirdate(Date hirdate) {
this.hirdate = hirdate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public float getSaly() {
return saly;
}
public void setSaly(float saly) {
this.saly = saly;
}
}
配置映射文件:
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>
<class name="emp.Dept" table="dept" catalog="myshop" schema="dbo">
<id name="id" type="java.lang.Integer">
<column name="deptno"></column>
<generator class="native"></generator>
</id>
<property name="dname" type="java.lang.String">
<column name="dname" length="20" not-null="true"/>
</property>
<property name="loc" type="java.lang.String">
<column name="loc" length="20" not-null="true"></column>
</property>
<!-- 配置set集合,name表示Dept类中的属性名称 -->
<!-- inverse 表示Emp类是否有主动权,为true表示没有主动权 -->
<!-- cascade 表示两个表的关联性 -->
<!-- lazy 表示是否延迟加载,为true表示延迟加载 -->
<set name="emp" inverse="true" cascade="all" lazy="true">
<key column="deptno" not-null="true"></key>
<one-to-many class="emp.Emp"/>
</set>
</class>
</hibernate-mapping>
Emp.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>
<class name="emp.Emp" table="emp" catalog="myshop" schema="dbo">
<id name="id" type="java.lang.Integer">
<column name="empno"></column>
<generator class="native"></generator>
</id>
<property name="ename" type="java.lang.String">
<column name="ename" length="20" not-null="true"></column>
</property>
<property name="saly" type="java.lang.Float">
<column name="saly" not-null="true"/>
</property>
<property name="hirdate" type="java.util.Date">
<column name="hirdate" not-null="false"></column>
</property>
<many-to-one name="dept" class="emp.Dept" fetch="select">
<column name="deptno" not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>
测试类:
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class Test {
private SessionFactory sessionFactory;
private Session session;
public Test()
{
sessionFactory=new Configuration().configure().buildSessionFactory();
session=sessionFactory.openSession();
}
public void doCreate()
{
Dept p=new Dept();
p.setDname("销售部");
p.setLoc("很好");
//创建两个与Dept相关联的类e,e1
Emp e=new Emp();
e.setEname("张三");
e.setSaly(1000.0f);
e.setHirdate(new Date());
Emp e2=new Emp();
e2.setEname("李四");
e2.setSaly(2000.0f);
e2.setHirdate(new Date());
//把两个Emp 对象添加到Set集合中
p.getEmp().add(e);
p.getEmp().add(e2);
//Emp类也要设置关联
e.setDept(p);
e2.setDept(p);
//保存Dept类同时,也会保存与Dept相关联的Emp类e,e1
session.save(p);
session.beginTransaction().commit();
session.close();
}
public void find2()
{
Dept d=(Dept)session.get(Dept.class, new Integer(1));
System.out.println(d.getDname());
System.out.println(d.getLoc());
}
public void find1()
{
String hql="FROM Dept d";
Query q=session.createQuery(hql);
Dept d=(Dept)q.uniqueResult();
Set<Emp> emp=d.getEmp();
Iterator<Emp> it=emp.iterator();
while(it.hasNext())
{
Emp e=it.next();
System.out.println(e.getEname());
System.out.println(e.getHirdate());
System.out.println(e.getSaly());
}
}
public void find3()
{
String hql="FROM Emp e WHERE e.dept.id=1";
Query q=session.createQuery(hql);
List emp=q.list();
for(int i=0;i<emp.size();i++)
{
Emp e=(Emp)emp.get(i);
System.out.println(e.getEname()+" "+e.getSaly());
}
}
public static void main(String[] args) {
//new Test().doCreate();
new Test().find1();
}
}