Hibernate映射一对多关系
Customer.java
package org.model;
import java.util.Set;
public class Customer {
private int c_id;//主键
private String c_name;
private Set orders;//一对多关系,然后持有Set集合
public int getC_id() {
return c_id;
}
public String getC_name() {
return c_name;
}
public Set getOrders() {
return orders;
}
public void setC_id(int c_id) {
this.c_id = c_id;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public void setOrders(Set orders) {
this.orders = orders;
}
}
Order.java
package org.model;
public class Order {
private Customer customer;//一个订单对一个顾客
private int o_id;
private String o_number;
public Customer getCustomer() {
return customer;
}
public int getO_id() {
return o_id;
}
public String getO_number() {
return o_number;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public void setO_id(int o_id) {
this.o_id = o_id;
}
public void setO_number(String o_number) {
this.o_number = o_number;
}
}
类写好,下一步是配置文件
Customer.hbm.xml【一对多关联】
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.model.Customer" table="customer">
<id name="c_id" column="u_id" >
<generator class="increment"></generator>
</id>
<property name="c_name" type="string" >
<column name="u_name" length="15"/>
</property>
<set name="orders" cascade="all" inverse="true" lazy="false">
<key column="customer_id"></key>
<one-to-many class="org.model.Order" /><!-- set元素类型order类型得 -->
</set>
</class>
</hibernate-mapping>
主键生成策略是自增increment
<set>元素表明orders这个属性是Set类型 cascade是级联属性 默认值为none,类似数据库中的级联操作 lazy指的是是否延迟检索
key子元素表明通过外键customer_id参照customer表
one to many 命名存储的是一组Order对象
Order.hbm.xml【多对一关联】
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.model.Order" table="order1">
<id name="o_id" column="o_id" >
<generator class="increment"></generator>
</id>
<property name="o_number" type="string" >
<column name="o_number" length="15"></column>
</property>
<many-to-one name="customer" class="org.model.Customer" column="customer_id"></many-to-one>
</class>
</hibernate-mapping>
可能我们会以为应该这么配置<property name="customer" column="customer_id">
解释:显然这个是错的,customer是一个类类型,而customer_id是一个整型,这当然是不匹配的,要使用<many-to-one>多对一
name:属性名 class:类名 not-null 如果为true 表示customer属性不允许为null---对应数据库就是加一个not null约束 还会影响运行时行为 是null么?有检验机制
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: org.model.Order.customer
【爆出这个异常】
hibernate帮我们产生的建立表的sql语句如下:
create table customer (u_id integer not null, u_name varchar(15), primary key (u_id))
create table order1 (o_id integer not null, o_number varchar(15), customer_id integer, primary key (o_id))
alter table order1 add index FKC3DF62A3D5851A89 (customer_id), add constraint FKC3DF62A3D5851A89 foreign key (customer_id) references customer (u_id)