在领域模型中,类与类之间最普通的关系就是关联关系。
在UML中,关联是有方向的:
例如:Customer与Order,一个用户能发出多个订单,而一个订单只能属于一个用户。
- 单向关联
1)从Order到Customer的关联是多对一关联;
定义Customer:
package com.dx.hibernate.onetomany; public class Customer {
private Integer customerId;
private String customerName; public Customer() {
} public Customer(String customerName) {
super();
this.customerName = customerName;
} public Integer getCustomerId() {
return customerId;
} public void setCustomerId(Integer customerId) {
this.customerId = customerId;
} public String getCustomerName() {
return customerName;
} public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-24 12:02:56 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.Customer" table="CUSTOMER">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="native" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
</class>
</hibernate-mapping>
定义Order:
package com.dx.hibernate.onetomany; public class Order {
private Integer orderId;
private String orderName;
private Customer customer; public Integer getOrderId() {
return orderId;
} public void setOrderId(Integer orderId) {
this.orderId = orderId;
} public String getOrderName() {
return orderName;
} public void setOrderName(String orderName) {
this.orderName = orderName;
} public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-24 12:02:56 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.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="com.dx.hibernate.onetomany.Customer">
<column name="CUSTOMER_ID" />
</many-to-one>
</class>
</hibernate-mapping>
初始化项目时,生成sql语句:
Hibernate: create table CUSTOMER (
CUSTOMER_ID integer not null auto_increment,
CUSTOMER_NAME varchar(255),
primary key (CUSTOMER_ID)
) engine=InnoDB
Hibernate: create table ORDERS (
ORDER_ID integer not null auto_increment,
ORDER_NAME varchar(255),
CUSTOMER_ID integer,
primary key (ORDER_ID)
) engine=InnoDB
Hibernate: alter table ORDERS
add constraint FKkdbly1ij6f4kqh378kfne6ilx
foreign key (CUSTOMER_ID)
references CUSTOMER (CUSTOMER_ID)
测试代码1:
package com.dx.hibernate.onetomany; import static org.junit.Assert.fail; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class TestMain {
private SessionFactory sessionFactory = null;
private Session session = null;
private Transaction transaction = null; @Before
public void init() {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build(); sessionFactory = metadata.getSessionFactoryBuilder().build();
session = sessionFactory.getCurrentSession();
transaction = session.beginTransaction();
} @Test
public void test() {
Customer customer = new Customer("customer1");
Order order1 = new Order("order1", customer);
Order order2 = new Order("order2", customer); session.save(customer);
session.save(order1);
session.save(order2);
} @After
public void destory() {
transaction.commit();
session.close();
sessionFactory.close();
}
}
测试执行sql:
Hibernate:
insert
into
CUSTOMER
(CUSTOMER_NAME)
values
(?)
Hibernate:
insert
into
ORDERS
(ORDER_NAME, CUSTOMER_ID)
values
(?, ?)
Hibernate:
insert
into
ORDERS
(ORDER_NAME, CUSTOMER_ID)
values
(?, ?)
测试代码2:
@Test
public void test() {
Order order = (Order) session.get(Order.class, 1); System.out.println(order.getOrderName());
System.out.println(order.getCustomer().getCustomerName());
}
测试结果:
测试通过,且查询语句为:
Hibernate:
select
order0_.ORDER_ID as ORDER_ID1_1_0_,
order0_.ORDER_NAME as ORDER_NA2_1_0_,
order0_.CUSTOMER_ID as CUSTOMER3_1_0_
from
ORDERS order0_
where
order0_.ORDER_ID=?
order1
Hibernate:
select
customer0_.CUSTOMER_ID as CUSTOMER1_0_0_,
customer0_.CUSTOMER_NAME as CUSTOMER2_0_0_
from
CUSTOMER customer0_
where
customer0_.CUSTOMER_ID=?
customer1
测试代码3:
@Test
public void test() {
Order order = (Order) session.get(Order.class, 1); System.out.println(order.getOrderName());
//System.out.println(order.getCustomer().getCustomerName()); session.close(); System.out.println(order.getCustomer().getCustomerName());
}
测试结果不通过,执行sql语句:
Hibernate:
select
order0_.ORDER_ID as ORDER_ID1_1_0_,
order0_.ORDER_NAME as ORDER_NA2_1_0_,
order0_.CUSTOMER_ID as CUSTOMER3_1_0_
from
ORDERS order0_
where
order0_.ORDER_ID=?
order1
测试4:
修改Order.hbm.xml
<many-to-one name="customer" class="com.dx.hibernate.onetomany.Customer" fetch="join">
<column name="CUSTOMER_ID" />
</many-to-one>
测试代码:
Order order = (Order) session.get(Order.class, 1); System.out.println(order.getOrderName());
System.out.println(order.getCustomer().getCustomerName());
测试通过,打印的sql语句:
Hibernate:
select
order0_.ORDER_ID as ORDER_ID1_1_0_,
order0_.ORDER_NAME as ORDER_NA2_1_0_,
order0_.CUSTOMER_ID as CUSTOMER3_1_0_,
customer1_.CUSTOMER_ID as CUSTOMER1_0_1_,
customer1_.CUSTOMER_NAME as CUSTOMER2_0_1_
from
ORDERS order0_
left outer join
CUSTOMER customer1_
on order0_.CUSTOMER_ID=customer1_.CUSTOMER_ID
where
order0_.ORDER_ID=?
测试代码5:
修改Order.hbm.xml
<many-to-one name="customer" class="com.dx.hibernate.onetomany.Customer" fetch="join">
<column name="CUSTOMER_ID" />
</many-to-one>
测试代码:
Order order = (Order) session.get(Order.class, 1); System.out.println(order.getOrderName());
System.out.println(order.getCustomer().getCustomerName()); session.close(); System.out.println(order.getCustomer().getCustomerName());
测试不通过。
2)从Customer到Order的关联是一对多关联。
Order.java
package com.dx.hibernate.onetomany; public class Order {
private Integer orderId;
private String orderName; public Order() {
super();
} public Order(String orderName) {
super();
this.orderName = orderName;
} public Integer getOrderId() {
return orderId;
} public void setOrderId(Integer orderId) {
this.orderId = orderId;
} public String getOrderName() {
return orderName;
} public void setOrderName(String orderName) {
this.orderName = orderName;
} }
Order.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-25 22:09:21 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.Order" table="ORDERS">
<id name="orderId" type="java.lang.Integer">
<column name="ORDER_ID" />
<generator class="native" />
</id>
<property name="orderName" type="java.lang.String">
<column name="ORDER_NAME" />
</property>
</class>
</hibernate-mapping>
Customer.java
package com.dx.hibernate.onetomany; import java.util.ArrayList;
import java.util.List; public class Customer {
private Integer customerId;
private String customerName;
private List<Order> orders = new ArrayList<Order>(); public Customer() {
} public Customer(String customerName) {
super();
this.customerName = customerName;
} public Integer getCustomerId() {
return customerId;
} public void setCustomerId(Integer customerId) {
this.customerId = customerId;
} public String getCustomerName() {
return customerName;
} public void setCustomerName(String customerName) {
this.customerName = customerName;
} public List<Order> getOrders() {
return orders;
} public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-25 22:09:21 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.Customer" table="CUSTOMER">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="native" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
<list name="orders" inverse="false" table="ORDERS" lazy="true">
<key>
<column name="CUSTOMER_ID" />
</key>
<list-index></list-index>
<one-to-many class="com.dx.hibernate.onetomany.Order" />
</list>
</class>
</hibernate-mapping>
TestMain.java
package com.dx.hibernate.onetomany; import static org.junit.Assert.fail; import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.query.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class TestMain {
private SessionFactory sessionFactory = null;
private Session session = null;
private Transaction transaction = null; @Before
public void init() {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build(); sessionFactory = metadata.getSessionFactoryBuilder().build();
session = sessionFactory.getCurrentSession();
transaction = session.beginTransaction();
} @SuppressWarnings("deprecation")
@Test
public void test() {
Order order1 = new Order("order1");
Order order2 = new Order("order2");
Customer customer = new Customer("customer1");
customer.getOrders().add(order1);
customer.getOrders().add(order2); session.save(customer);
session.save(order1);
session.save(order2); Customer customerFetch = (Customer) session.get(Customer.class, 1); System.out.println(customerFetch.getCustomerName());
System.out.println(customerFetch.getOrders().size()); } @After
public void destory() {
transaction.commit();
session.close();
sessionFactory.close();
}
}
测试结果:通过,打印信息如下:
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@320e400] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: create table CUSTOMER (
CUSTOMER_ID integer not null auto_increment,
CUSTOMER_NAME varchar(255),
primary key (CUSTOMER_ID)
) engine=InnoDB
Hibernate: create table ORDERS (
ORDER_ID integer not null auto_increment,
ORDER_NAME varchar(255),
CUSTOMER_ID integer,
orders_ORDER integer,
primary key (ORDER_ID)
) engine=InnoDB
Hibernate: alter table ORDERS
add constraint FKkdbly1ij6f4kqh378kfne6ilx
foreign key (CUSTOMER_ID)
references CUSTOMER (CUSTOMER_ID)
Hibernate:
insert
into
CUSTOMER
(CUSTOMER_NAME)
values
(?)
Hibernate:
insert
into
ORDERS
(ORDER_NAME)
values
(?)
Hibernate:
insert
into
ORDERS
(ORDER_NAME)
values
(?)
customer1
2
Hibernate:
update
ORDERS
set
CUSTOMER_ID=?,
orders_ORDER=?
where
ORDER_ID=?
Hibernate:
update
ORDERS
set
CUSTOMER_ID=?,
orders_ORDER=?
where
ORDER_ID=?
- 双向关联
Order.java
package com.dx.hibernate.onetomany; public class Order {
private Integer orderId;
private String orderName;
private Customer customer; public Order() {
super();
} public Order(String orderName) {
super();
this.orderName = orderName;
} public Integer getOrderId() {
return orderId;
} public void setOrderId(Integer orderId) {
this.orderId = orderId;
} public String getOrderName() {
return orderName;
} public void setOrderName(String orderName) {
this.orderName = orderName;
} public Customer getCustomer() {
return customer;
} public void setCustomer(Customer customer) {
this.customer = customer;
} }
Order.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-25 22:09:21 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.Order" table="ORDERS">
<id name="orderId" type="java.lang.Integer">
<column name="ORDER_ID" />
<generator class="native" />
</id>
<property name="orderName" type="java.lang.String">
<column name="ORDER_NAME" />
</property>
<many-to-one name="customer" class="com.dx.hibernate.onetomany.Customer" lazy="proxy">
<column name="CUSTOMER_ID"></column>
</many-to-one>
</class>
</hibernate-mapping>
Customer.java
package com.dx.hibernate.onetomany; import java.util.ArrayList;
import java.util.List; public class Customer {
private Integer customerId;
private String customerName;
private List<Order> orders = new ArrayList<Order>(); public Customer() {
} public Customer(String customerName) {
super();
this.customerName = customerName;
} public Integer getCustomerId() {
return customerId;
} public void setCustomerId(Integer customerId) {
this.customerId = customerId;
} public String getCustomerName() {
return customerName;
} public void setCustomerName(String customerName) {
this.customerName = customerName;
} public List<Order> getOrders() {
return orders;
} public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-25 22:09:21 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.dx.hibernate.onetomany.Customer" table="CUSTOMER">
<id name="customerId" type="java.lang.Integer">
<column name="CUSTOMER_ID" />
<generator class="native" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMER_NAME" />
</property>
<list name="orders" inverse="false" table="ORDERS" lazy="true">
<key>
<column name="CUSTOMER_ID" />
</key>
<list-index></list-index>
<one-to-many class="com.dx.hibernate.onetomany.Order" />
</list>
</class>
</hibernate-mapping>
测试1
package com.dx.hibernate.onetomany; import static org.junit.Assert.fail; import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.query.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class TestMain {
private SessionFactory sessionFactory = null;
private Session session = null;
private Transaction transaction = null; @Before
public void init() {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build(); sessionFactory = metadata.getSessionFactoryBuilder().build();
session = sessionFactory.getCurrentSession();
transaction = session.beginTransaction();
} @SuppressWarnings("deprecation")
@Test
public void test() {
Order order1 = new Order("order1");
Order order2 = new Order("order2");
Customer customer = new Customer("customer1");
customer.getOrders().add(order1);
customer.getOrders().add(order2); session.save(customer);
session.save(order1);
session.save(order2); Customer customerFetch = (Customer) session.get(Customer.class, 1); System.out.println(customerFetch.getCustomerName());
System.out.println(customerFetch.getOrders().size()); } @After
public void destory() {
transaction.commit();
session.close();
sessionFactory.close();
}
}
测试打印信息:
Hibernate: create table CUSTOMER (
CUSTOMER_ID integer not null auto_increment,
CUSTOMER_NAME varchar(255),
primary key (CUSTOMER_ID)
) engine=InnoDB
Hibernate: create table ORDERS (
ORDER_ID integer not null auto_increment,
ORDER_NAME varchar(255),
CUSTOMER_ID integer,
orders_ORDER integer,
primary key (ORDER_ID)
) engine=InnoDB
Hibernate: alter table ORDERS
add constraint FKkdbly1ij6f4kqh378kfne6ilx
foreign key (CUSTOMER_ID)
references CUSTOMER (CUSTOMER_ID)
Hibernate:
insert
into
CUSTOMER
(CUSTOMER_NAME)
values
(?)
Hibernate:
insert
into
ORDERS
(ORDER_NAME, CUSTOMER_ID)
values
(?, ?)
Hibernate:
insert
into
ORDERS
(ORDER_NAME, CUSTOMER_ID)
values
(?, ?)
customer1
2
Hibernate:
update
ORDERS
set
CUSTOMER_ID=?,
orders_ORDER=?
where
ORDER_ID=?
Hibernate:
update
ORDERS
set
CUSTOMER_ID=?,
orders_ORDER=?
where
ORDER_ID=?
测试2:
@Test
public void testUpdate(){
Customer customerFetch = (Customer) session.get(Customer.class, 1);
customerFetch.getOrders().listIterator().next().setOrderName("ABC");
}
测试输出:
Hibernate:
select
customer0_.CUSTOMER_ID as CUSTOMER1_0_0_,
customer0_.CUSTOMER_NAME as CUSTOMER2_0_0_
from
CUSTOMER customer0_
where
customer0_.CUSTOMER_ID=?
Hibernate:
select
orders0_.CUSTOMER_ID as CUSTOMER3_1_0_,
orders0_.ORDER_ID as ORDER_ID1_1_0_,
orders0_.orders_ORDER as orders_O4_0_,
orders0_.ORDER_ID as ORDER_ID1_1_1_,
orders0_.ORDER_NAME as ORDER_NA2_1_1_,
orders0_.CUSTOMER_ID as CUSTOMER3_1_1_
from
ORDERS orders0_
where
orders0_.CUSTOMER_ID=?
Hibernate:
update
ORDERS
set
ORDER_NAME=?,
CUSTOMER_ID=?
where
ORDER_ID=?
测试3:
@Test
public void testRemove() {
Customer customerFetch = (Customer) session.get(Customer.class, 2);
Order order = customerFetch.getOrders().listIterator().next();
session.remove(order);
}
测试输出:
Hibernate:
select
customer0_.CUSTOMER_ID as CUSTOMER1_0_0_,
customer0_.CUSTOMER_NAME as CUSTOMER2_0_0_
from
CUSTOMER customer0_
where
customer0_.CUSTOMER_ID=?
Hibernate:
select
orders0_.CUSTOMER_ID as CUSTOMER3_1_0_,
orders0_.ORDER_ID as ORDER_ID1_1_0_,
orders0_.orders_ORDER as orders_O4_0_,
orders0_.ORDER_ID as ORDER_ID1_1_1_,
orders0_.ORDER_NAME as ORDER_NA2_1_1_,
orders0_.CUSTOMER_ID as CUSTOMER3_1_1_
from
ORDERS orders0_
where
orders0_.CUSTOMER_ID=?
Hibernate:
delete
from
ORDERS
where
ORDER_ID=?