第九章 关系映射 多对一关联映射
2013-06-06 10:47
387 查看
如:department与employee
employee中有一个department_id的外键
Department:
public class Department implements Serializable {
private Integer id;
private String name;
public Department() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Employee:
public class Employee implements Serializable {
private Integer id;
private String name;
private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Department.hbm.xml:(与普通的映射文件一样)
<hibernate-mapping>
<class name="cn.framelife.hibernate.entity.Department" table="department"
catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>
Employee.hbm.xml:
<hibernate-mapping>
<class name="cn.framelife.hibernate.entity.Employee" table="employee"
catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
<many-to-one name="department" column="department_id"></many-to-one>
</class>
</hibernate-mapping>
增加:
查询:
查询employee的时候可以得到外键关联的department对象。
employee中有一个department_id的外键
Department:
public class Department implements Serializable {
private Integer id;
private String name;
public Department() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Employee:
public class Employee implements Serializable {
private Integer id;
private String name;
private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Department.hbm.xml:(与普通的映射文件一样)
<hibernate-mapping>
<class name="cn.framelife.hibernate.entity.Department" table="department"
catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>
Employee.hbm.xml:
<hibernate-mapping>
<class name="cn.framelife.hibernate.entity.Employee" table="employee"
catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
<many-to-one name="department" column="department_id"></many-to-one>
</class>
</hibernate-mapping>
增加:
transaction = session.beginTransaction(); Department department = new Department(); department.setName("bb"); session.save(department); Employee employee = new Employee(); employee.setDepartment(department); employee.setName("li"); session.save(employee); transaction.commit();
查询:
查询employee的时候可以得到外键关联的department对象。
相关文章推荐
- 第九章 关系映射 集合关联映射
- 第九章 关系映射 继承关联映射
- 第九章 关系映射 一对多关联映射
- 第九章 关系映射 多对多关联映射
- JPA关系映射系列五:many-to-many 关联表存在额外字段关系映射
- 关系映射的双向关联的多对多
- Hibernate实体关系映射:单向主键一对一关联
- 【Hibernate四】关系映射之一对多双向关联
- hibernate 一对多 关系关联映射
- Hibernate关系映射级别注解(多对一单向外键关联)
- Hibernate关系映射 --- 一对一实例分析(双向关联,是基于主键的)
- Hibernate关系映射:多对多关联
- Hibernate之对象关系映射03一对一单向外键关联
- 【HIbernate框架学习】:Hibernate对象关系映射之一对一关联映射(一)
- Hibernate 映射关系 ---One2One 外键关联
- Hibernate 映射关系 ---Many2Many 单向关联
- Hibernate(七)实现多对多关联映射关系
- Hibernate关系映射(四)一对多单向关联@OneToMany Annotation方式
- hibernate-关系映射一对多(多对一)双向关联)-10
- Hibernate关系映射(3)_一对一单向主键关联