This series of blog summarized here: Hibernate Summary
Many to one association mapping
Source project file: hibernate4.3_04
Many to one association mapping
Examples of the use of staff and team
Class Diagram:
Database Diagram:
1, to create a model
Create more end
package com.wyx.hiber.model;
import java.util.Date;
public class Emp
{
private Integer empNo;
private String ename;
private Date birthday;
private Integer gender;
private String address;
/**
* 引入一的一端的类作为成员,指定多对一的关系
*
*/
private Team team;
public Team getTeam()
{
return team;
}
public void setTeam(Team team)
{
this.team = team;
}
public Integer getEmpNo()
{
return empNo;
}
public void setEmpNo(Integer empNo)
{
this.empNo = empNo;
}
public String getEname()
{
return ename;
}
public void setEname(String ename)
{
this.ename = ename;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Integer getGender()
{
return gender;
}
public void setGender(Integer gender)
{
this.gender = gender;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Override
public String toString()
{
return "Emp [empNo=" + empNo + ", ename=" + ename + ", birthday="
+ birthday + ", gender=" + gender + ", address=" + address
+ "]";
}
}
Creating a end
package com.wyx.hiber.model;
public class Team
{
private Integer tId;
private String tName;
private String loc;
public Integer gettId()
{
return tId;
}
public void settId(Integer tId)
{
this.tId = tId;
}
public String gettName()
{
return tName;
}
public void settName(String tName)
{
this.tName = tName;
}
public String getLoc()
{
return loc;
}
public void setLoc(String loc)
{
this.loc = loc;
}
@Override
public String toString()
{
return "Team [tId=" + tId + ", tName=" + tName + ", loc=" + loc + "]";
}
}
2, configuration mapping file
Configure multiple end
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wyx.hiber.model">
<class name="Emp" table="t_emp">
<id name="empNo" column="emp_no">
<generator class="native"></generator>
</id>
<property name="ename"></property>
<property name="birthday"></property>
<property name="gender"></property>
<property name="address"></property>
<!--
指定多对一的关系
name:多的一端(emp)的类中的一的类(team)的属性名
column:建表后多的一端(t_emp)中的外键
cascade:delete切记不要使用
save-update级联保存或更新
all包含了以上两种
none不使用级联(默认的)
-->
<many-to-one name="team" column="t_id" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
A configuration of an end
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wyx.hiber.model">
<class name="Team" table="t_team">
<id name="tId" column="t_id">
<generator class="native"></generator>
</id>
<property name="tName" column="t_name"></property>
<property name="loc"></property>
</class>
</hibernate-mapping>
After you create the Hibernate mapping file you want to register to our hibernate.cfg.xml, and then export the database can be.
3, many-test
Storage
public void testAdd()
{
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
try
{
// 创建一支球队
Team t = new Team();
t.settName("公牛");
t.setLoc("芝加哥");
// 保存球队
session.save(t);
// 创建员工
Emp emp = new Emp();
emp.setEname("乔丹");
emp.setGender(1);
emp.setBirthday(new Date());
emp.setAddress("芝加哥");
Emp emp1 = new Emp();
emp1.setEname("菲尔");
emp1.setGender(1);
emp1.setBirthday(new Date());
emp1.setAddress("芝加哥");
// 设置员工和球队的关系
emp.setTeam(t);
emp1.setTeam(t);
// 保存员工
session.save(emp);
session.save(emp1);
tx.commit();
} catch (Exception e)
{
e.printStackTrace();
tx.rollback();
} finally
{
HibernateUtils.closeResource(session);
}
}
Note: be sure to keep a hold of one end of the multi-end time, if one end is a transient object error will be
correct output
saved many to one relationship when you can also use cascading manner
using cascaded save there is no need to save a separate one of the end.
Inquire
Use get to query
@Test
public void testQuery()
{
Session session = HibernateUtils.getSession();
try
{
// 调用get方法来查询指定要查询的类和id
Emp emp = (Emp) session.get(Emp.class, 5);
System.out.println(emp);
System.out.println(emp.getTeam());
} catch (Exception e)
{
e.printStackTrace();
} finally
{
HibernateUtils.closeResource(session);
}
}
Use load to query
@Test
public void testQuery1()
{
Session session = HibernateUtils.getSession();
try
{
// 调用load方法来查询指定要查询的类和id
Emp emp = (Emp) session.load(Emp.class, 5);
System.out.println(emp);
System.out.println(emp.getTeam());
} catch (Exception e)
{
e.printStackTrace();
} finally
{
HibernateUtils.closeResource(session);
}
}
If wrong, please correct me!