单向 多对一
多个Order指向同一个Customer
Customer.hbm.xml
Order.hbm.xml
双向 多对一
Customer.hbm.xml
多个Order指向同一个Customer
Customer.hbm.xml
<hibernate-mapping>
<class name="com.my.n21.Customer" table="CUSTOMER">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="assigned" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
</class>
</hibernate-mapping>
Order.hbm.xml
<!--
映射多对一的关联关系,使用 many-to-one 来映射多对一的关联关系
name: 属性的名字
class: 属性对应的类名
column: 数据表中的外键的名字
-->
<hibernate-mapping package="com.my.n21">
<class name="Order" table="ORDERS">
<id name="orderId" type="java.lang.Integer">
<column name="ORDER_ID" />
<generator class="assigned" />
</id>
<property name="orderName" type="java.lang.String">
<column name="ORDER_NAME" />
</property>
<many-to-one name="customer" class="Customer" column="CUSTOMER_ID">
</many-to-one>
</class>
</hibernate-mapping>
============================创建表============================
Hibernate:
create table CUSTOMER (
CUSTOMER_ID integer not null,
CUSTOMER_NAME varchar(255),
primary key (CUSTOMER_ID)
)
Hibernate:
create table ORDERS (
ORDER_ID integer not null,
ORDER_NAME varchar(255),
CUSTOMER_ID integer,
primary key (ORDER_ID)
)
Hibernate:
alter table ORDERS
add index FK_57wwsm6wqqkcr1amp4dtsk5bs (CUSTOMER_ID),
add constraint FK_57wwsm6wqqkcr1amp4dtsk5bs
foreign key (CUSTOMER_ID)
references CUSTOMER (CUSTOMER_ID)
--------------------------------------------------------------------------------
@Test
public void testGet(){
Order order = (Order)session.get(Order.class, 1);
System.out.println(order.getOrderName());
//这里的customer是一个代理对象
System.out.println(order.getCustomer().getClass().getName());
}
@Test
public void testSave() throws IOException{
Customer customer = new Customer();
customer.setCustomerId(1);
customer.setCustomerName("One");
Order r1 = new Order();
r1.setOrderId(1);
r1.setOrderName("Order1");
r1.setCustomer(customer);
Order r2 = new Order();
r2.setOrderId(2);
r2.setCustomer(customer);
r2.setOrderName("Order2");
session.save(customer);
session.save(r1);
session.save(r2);
}
@Test
public void testDel(){
//不设定级联的情况下:在Customer被Order所应用的情况下,Customer是不能删除的
Customer customer = (Customer) session.get(Customer.class, 1);
session.delete(customer);
}
@Test
public void testUpdate(){
Order order = (Order) session.get(Order.class, 1);
order.getCustomer().setCustomerName("Mark");
}
双向 多对一
Customer.hbm.xml
<hibernate-mapping>
<class name="com.my.nj1.Customer" table="CUSTOMER">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="assigned" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
<!--
set:
inverse: 指定由哪一方来维护关联关系. 通常设置为 true, 以指定由多的一端来维护关联关系
在 1-n 关系中,将 n 方设为主控方将有助于性能改善。
cascade 设定级联操作. 开发时不建议设定该属性.
order-by 在查询时对集合中的元素进行排序, order-by 中使用的是表的字段名
-->
<set name="orders" table="ORDERS" inverse="true" order-by="ORDER_NAME DESC">
<key>
<column name="CUSTOMER_ID" />
</key>
<!-- 指定映射类型 -->
<one-to-many class="com.my.nj1.Order" />
</set>
</class>
</hibernate-mapping>
=========================创建表===============================
Hibernate:
create table CUSTOMER (
CUSTOMER_ID integer not null auto_increment,
CUSTOMER_NAME varchar(255),
primary key (CUSTOMER_ID)
)
Hibernate:
create table ORDERS (
ORDER_ID integer not null auto_increment,
ORDER_NAME varchar(255),
CUSTOMER_ID integer,
primary key (ORDER_ID)
)
Hibernate:
alter table ORDERS
add index FK_57wwsm6wqqkcr1amp4dtsk5bs (CUSTOMER_ID),
add constraint FK_57wwsm6wqqkcr1amp4dtsk5bs
foreign key (CUSTOMER_ID)
references CUSTOMER (CUSTOMER_ID)
--------------------------------------------------------------------------------
@Test
public void testSave(){
Customer customer = new Customer();
customer.setCustomerName("A");
Order order1= new Order();
order1.setOrderName("1");
Order order2 = new Order();
order2.setOrderName("2");
//设定关联关系
order1.setCustomer(customer);
order2.setCustomer(customer);
customer.getOrders().add(order1);
customer.getOrders().add(order2);
//建议设定 set 的 inverse=true, 建议先插入 1 的一端, 后插入多的一端
//好处是不会多出 UPDATE 语句
session.save(customer);
session.save(order1);
session.save(order2);
}
@Test
public void testGetMany(){
Order order = (Order) session.get(Order.class, 1);
System.out.println(order.getOrderName());
System.out.println(order.getCustomer().getClass().getName());
// 在需要使用到关联的对象时, 才发送对应的 SQL 语句.
Customer customer = order.getCustomer();
System.out.println(customer.getCustomerName());
}
@Test
public void getOne(){
Customer customer = (Customer) session.get(Customer.class, 1);
System.out.println(customer.getOrders().getClass());
//class org.hibernate.collection.internal.PersistentSet
System.out.println(customer.getOrders().size());
//2
}
@Test
public void testUpate(){
Customer customer = (Customer) session.get(Customer.class, 1);
customer.getOrders().iterator().next().setOrderName("OrderbyCustomer");
}
@Test
public void testDel(){
//不设定级联的情况下:在Customer被Order所应用的情况下,Customer是不能删除的
Customer customer = (Customer) session.get(Customer.class, 1);
session.delete(customer);
}