要点:
1.也就是有个主表,然后另外一个表引用主表中的某个字段作为外键
2.在副对象中,引用主表对象.然后在mapping文件中,映射多对一关系,并制定各自的属性名,类名,外键名
<!--
映射多对一的关联关系。 使用 many-to-one 来映射多对一的关联关系
name: 多这一端:关联的一那一端的属性的名字
class: 一那一端:的属性对应的类名
column: 一那一端在多的一端对应的数据表中的外键的名字,可以随便取
<many-to-one name="customer" class="Customer">
<column name="CUSTOMER_ID" />
</many-to-one>
表名用了ORDER,使得出现了错误,改为ORDERS即可
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER (ORDER_NAME, CUSTOMER_ID) values ('order1', 1)' at line 1
-->
<many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one>
package com.hgh.hibernate.nto1;
public class Customer {
private Integer customerId;
private String 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;
}
}
package com.hgh.hibernate.nto1;
public class Order1 {
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-4-13 11:17:27 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="com.hgh.hibernate.nto1">
<class name="Order1" 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: 多这一端:关联的一那一端的属性的名字
class: 一那一端:的属性对应的类名
column: 一那一端在多的一端对应的数据表中的外键的名字,可以随便取
<many-to-one name="customer" class="Customer">
<column name="CUSTOMER_ID" />
</many-to-one>
表名用了ORDER,使得出现了错误,改为ORDERS即可
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER (ORDER_NAME, CUSTOMER_ID) values ('order1', 1)' at line 1
-->
<many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one>
</class>
</hibernate-mapping>
<?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-4-13 11:17:27 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hgh.hibernate.nto1.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>
package com.hgh.hibernate.nto1;
import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SessionTest {
private SessionFactory SessionFactory;
private ServiceRegistry ServiceRegistry;
private Transaction transaction;
private Session session;
@Test
public void testDelete(){
Customer customer = (Customer) session.get(Customer.class, 1);
session.delete(customer);
}
@Test
public void testUpdateManyToOne(){
Customer customer = (Customer) session.get(Customer.class, 1);
customer.setCustomerName("313");
session.update(customer);
}
@Test
public void testManyToOneGet(){
Order1 order1 = (Order1) session.get(Order1.class, 1);
System.out.println(order1.getOrderName());
//com.hgh.hibernate.nto1.Customer_$$_javassist_3代理对象,需要使用的时候再填充
System.out.println(order1.getCustomer().getClass().getName());
Customer customer = order1.getCustomer();
//2. 在需要使用到关联的对象时, 才发送对应的 SQL 语句.
System.out.println(customer.getCustomerName());
//3. 在查询 Customer 对象时, 由多的一端导航到 1 的一端时,
//若此时 session 已被关闭, 则默认情况下
//会发生 LazyInitializationException 异常
//4. 获取 Order 对象时, 默认情况下, 其关联的 Customer 对象是一个代理对象!
}
@Test
public void testNtoOne(){
Customer customer = new Customer();
customer.setCustomerName("customer2");
Order1 order = new Order1();
Order1 order2 = new Order1();
order.setOrderName("order33");
order2.setOrderName("order44");
order.setCustomer(customer);
order2.setCustomer(customer);
//执行 save 操作: 先插入 Customer, 再插入 Order, 3 条 INSERT
//先插入 1 的一端, 再插入 n 的一端, 只有 INSERT 语句.
// session.save(customer);
// session.save(order);
// session.save(order2);
//先插入 Order, 再插入 Customer. 3 条 INSERT, 2 条 UPDATE
//先插入 n 的一端, 再插入 1 的一端, 会多出 UPDATE 语句!
//因为在插入多的一端时, 无法确定 1 的一端的外键值. 所以只能等 1 的一端插入后, 再额外发送 UPDATE 语句.
//推荐先插入 1 的一端, 后插入 n 的一端
session.save(order);
session.save(order2);
session.save(customer);
}
@Before
public void init(){
System.out.println("init");
Configuration configuration = new Configuration().configure();
ServiceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
SessionFactory = configuration.buildSessionFactory(ServiceRegistry);
session = SessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destroy(){
System.out.println("destroy");
transaction.commit();
session.close();
SessionFactory.close();
}
}