Customers表(一) 对 Orders表(多)
1.那么Orders表中就有Customers表的主键,也就是说Orders表的外键是
Customers表的主键
2.打开Orders表对应的VO:AbstractOrders.java文件,删除外键(Customers表
的主键)的Set(),Get()方法
3.创建一个类型为Customers对象的属性
4.配置Orders.hbm.xml映射文件。将 将<property name="customerId"
column="CUSTOMER_ID" type="java.lang.Long" />
改为<many-to-one name="customers" column="CUSTOMER_ID"
class="com.Customers" not-null="true" />
2---4步 MyEclipse4已经为我们全部自动生成!!!不用自己设置!
一对多双关联关系:
1。修改 Customers 对像(AbstractCustomers.java文件)增加一 orders 属性,
代码如下:private Set orders = new HashSet();跟Set(),Get()方法;
2、修改配置映射文件(Customers.hbm.xml)
在<property...>后面跟</class>前面加上
[code]<set
name="orders"
cascade="save-update">
<key column="CUSTOMER_ID" />
<one-to-many class="com.Orders" />
</set>[/code]
级联保存
原文件内容:
[code]<many-to-one
name="customers"
column="CUSTOMER_ID"
class="com.Customers"
not-null="true" />[/code]
修改为: 绿色字体部份为增加之处
[code] <many-to-one
name="customers"
column="CUSTOMER_ID"
class="com.Customers"
cascade="save-update"
not-null="true" />[/code]
//session.save(customers); 将此部份将关闭
映射一对多双关联关系
1。修改 Customers 对像(AbstractCustomers.java文件)
增加一 orders 属性,代码如下:
private Set orders = new HashSet();
这样配置以后如果要查询 Customers 所对应的所有 Orders 对像,只需要调用customer.getOrders()方法。
2。修改配置映射文件(Customers.hbm.xml)
源文件片断:(绿色字体为新增部份)
[code] <class name="Customers" table="customers">
<id name="id" column="ID" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<property name="age" column="AGE" type="java.lang.Integer" />
<set
name="orders"
cascade="save-update">
<key column="CUSTOMER_ID" />
<one-to-many class="com.Orders" />
</set>
</class>[/code]
MyEclipse已经为我们建立好第一跟第二步了,只需在第二步加上cascade="save-update"属性就行了!
3。修改 Orders 对你的 equals 和 hashCode 方法
[code]public int hashCode() {
if (this.hashValue == 0) {
int result = 17;
int idValue = this.getId() == null ? 0 : this.getId().hashCode();
result = result * 37 + idValue;
idValue = this.getOrderNumber() == null ? 0 : this.getCustomers()
.hashCode();
result = result * 37 + idValue;
idValue = this.getPrice() == null ? 0 : this.getPrice().hashCode();
result = result * 37 + idValue;
idValue = this.getCustomers() == null ? 0 : this.getCustomers()
.hashCode();
result = result * 37 + idValue;
this.hashValue = result;
}
return this.hashValue;
}
public boolean equals(Object rhs) {
if (rhs == null)
return false;
if (!(rhs instanceof Orders))
return false;
Orders that = (Orders) rhs;
if (this.getId() != null && that.getId() != null) {
if (!this.getId().equals(that.getId()))
return false;
if (!this.getOrderNumber().equals(that.getOrderNumber()))
return false;
if (!this.getPrice().equals(that.getPrice()))
return false;
if (!this.getCustomers().equals(that.getCustomers()))
return false;
}
return true;
}[/code]
在这里修改 equals 和 hashCode 方法的目的是为了使用Orders orders1 = new Orders()方法时和 Orders orders2 = new Orders() 创建的这两个 Orders 对你为两个不同的对像,如果不增加这几个判断,当创建新的 Orders 对像时,因为 Orders对像的 ID属性值为 null ,当创建第二个 Orders对像时,它的 ID属性值也为 null,hibernate 会将它们认为是同一个对像,当执行session.save()方法时只会保存一个orders对像。
级联查询、级联修改、级联删除都有一个参数“customer_id”可以指定将要查询的Customers对像的ID值
修改 Customers.hbm.xml 文件,绿色字体就修改部份
[code] <set
name="orders"
cascade="all-delete-orphan"
inverse="true">
<key column="CUSTOMER_ID" />
<one-to-many class="com.Orders" />
</set>[/code]
cascade="all-delete-orphan"这一设定值代表可以执行所有有联级方法,如保存、删除
1.那么Orders表中就有Customers表的主键,也就是说Orders表的外键是
Customers表的主键
2.打开Orders表对应的VO:AbstractOrders.java文件,删除外键(Customers表
的主键)的Set(),Get()方法
3.创建一个类型为Customers对象的属性
4.配置Orders.hbm.xml映射文件。将 将<property name="customerId"
column="CUSTOMER_ID" type="java.lang.Long" />
改为<many-to-one name="customers" column="CUSTOMER_ID"
class="com.Customers" not-null="true" />
2---4步 MyEclipse4已经为我们全部自动生成!!!不用自己设置!
一对多双关联关系:
1。修改 Customers 对像(AbstractCustomers.java文件)增加一 orders 属性,
代码如下:private Set orders = new HashSet();跟Set(),Get()方法;
2、修改配置映射文件(Customers.hbm.xml)
在<property...>后面跟</class>前面加上
[code]<set
name="orders"
cascade="save-update">
<key column="CUSTOMER_ID" />
<one-to-many class="com.Orders" />
</set>[/code]
级联保存
原文件内容:
[code]<many-to-one
name="customers"
column="CUSTOMER_ID"
class="com.Customers"
not-null="true" />[/code]
修改为: 绿色字体部份为增加之处
[code] <many-to-one
name="customers"
column="CUSTOMER_ID"
class="com.Customers"
cascade="save-update"
not-null="true" />[/code]
//session.save(customers); 将此部份将关闭
映射一对多双关联关系
1。修改 Customers 对像(AbstractCustomers.java文件)
增加一 orders 属性,代码如下:
private Set orders = new HashSet();
这样配置以后如果要查询 Customers 所对应的所有 Orders 对像,只需要调用customer.getOrders()方法。
2。修改配置映射文件(Customers.hbm.xml)
源文件片断:(绿色字体为新增部份)
[code] <class name="Customers" table="customers">
<id name="id" column="ID" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<property name="age" column="AGE" type="java.lang.Integer" />
<set
name="orders"
cascade="save-update">
<key column="CUSTOMER_ID" />
<one-to-many class="com.Orders" />
</set>
</class>[/code]
MyEclipse已经为我们建立好第一跟第二步了,只需在第二步加上cascade="save-update"属性就行了!
3。修改 Orders 对你的 equals 和 hashCode 方法
[code]public int hashCode() {
if (this.hashValue == 0) {
int result = 17;
int idValue = this.getId() == null ? 0 : this.getId().hashCode();
result = result * 37 + idValue;
idValue = this.getOrderNumber() == null ? 0 : this.getCustomers()
.hashCode();
result = result * 37 + idValue;
idValue = this.getPrice() == null ? 0 : this.getPrice().hashCode();
result = result * 37 + idValue;
idValue = this.getCustomers() == null ? 0 : this.getCustomers()
.hashCode();
result = result * 37 + idValue;
this.hashValue = result;
}
return this.hashValue;
}
public boolean equals(Object rhs) {
if (rhs == null)
return false;
if (!(rhs instanceof Orders))
return false;
Orders that = (Orders) rhs;
if (this.getId() != null && that.getId() != null) {
if (!this.getId().equals(that.getId()))
return false;
if (!this.getOrderNumber().equals(that.getOrderNumber()))
return false;
if (!this.getPrice().equals(that.getPrice()))
return false;
if (!this.getCustomers().equals(that.getCustomers()))
return false;
}
return true;
}[/code]
在这里修改 equals 和 hashCode 方法的目的是为了使用Orders orders1 = new Orders()方法时和 Orders orders2 = new Orders() 创建的这两个 Orders 对你为两个不同的对像,如果不增加这几个判断,当创建新的 Orders 对像时,因为 Orders对像的 ID属性值为 null ,当创建第二个 Orders对像时,它的 ID属性值也为 null,hibernate 会将它们认为是同一个对像,当执行session.save()方法时只会保存一个orders对像。
级联查询、级联修改、级联删除都有一个参数“customer_id”可以指定将要查询的Customers对像的ID值
修改 Customers.hbm.xml 文件,绿色字体就修改部份
[code] <set
name="orders"
cascade="all-delete-orphan"
inverse="true">
<key column="CUSTOMER_ID" />
<one-to-many class="com.Orders" />
</set>[/code]
cascade="all-delete-orphan"这一设定值代表可以执行所有有联级方法,如保存、删除