Set元素中的inverse属性
- 默认inverse = false,子表的外键由父表来进行维护,会出现多条UPDATE语句,用父表的主键来更新子表的外键,这样子表的外键就不会为null
- 当inverse = true时,子表的外键由子表来维护,不会出现update语句,这样外键可能会出现null
注意:在多对多的情况下,inverse一定要设置为true,在任何一方都可以。
代码如下:
分别创建order表及orderline表
CREATE TABLE t_order(
id NUMBER(10) PRIMARY KEY,
ordered_date DATE NOT NULL,
shipped_date DATE,
total NUMBER(10,2)
);
CREATE TABLE t_orderline(
id NUMBER(10) PRIMARY KEY,
price NUMBER(10,2),
quantity NUMBER(10),
product VARCHAR2(30),
order_id NUMBER(10) REFERENCES t_order(id)
);
CREATE SEQUENCE t_order_seq
START WITH 1
INCREMENT BY 1;
CREATE SEQUENCE t_orderline_seq
START WITH 1
INCREMENT BY 1;
Order类:
package com.iotek.basic.association.one2many.pojo;
import java.io.Serializable;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Order implements Serializable {
private static final long serialVersionUID = 1807690077527938744L;
private Long id;
private Date orderedDate;
private Date shippedDate;
private Double total;
private Set<Orderline> orderlines = new HashSet<Orderline>();
public Order() {}
public Order(Long id, Date orderedDate, Date shippedDate, Double total, Set<Orderline> orderlines) {
super();
this.id = id;
this.orderedDate = orderedDate;
this.shippedDate = shippedDate;
this.total = total;
this.orderlines = orderlines;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getOrderedDate() {
return orderedDate;
}
public void setOrderedDate(Date orderedDate) {
this.orderedDate = orderedDate;
}
public Date getShippedDate() {
return shippedDate;
}
public void setShippedDate(Date shippedDate) {
this.shippedDate = shippedDate;
}
public Double getTotal() {
return total;
}
public void setTotal(Double total) {
this.total = total;
}
public Set<Orderline> getOrderlines() {
return orderlines;
}
public void setOrderlines(Set<Orderline> orderlines) {
this.orderlines = orderlines;
}
@Override
public String toString() {
return "Order [id=" + id + ", orderedDate=" + orderedDate + ", shippedDate=" + shippedDate + ", total=" + total
+ ", orderlines=" + orderlines + "]";
}
}
Orderline类:
package com.iotek.basic.association.one2many.pojo;
import java.io.Serializable;
public class Orderline implements Serializable {
private static final long serialVersionUID = -3664371204911964913L;
private Long id;
private Double price;
private Long quantity;
private String product;
private Order order;
public Orderline() {}
public Orderline(Long id, Double price, Long quantity, String product, Order order) {
super();
this.id = id;
this.price = price;
this.quantity = quantity;
this.product = product;
this.order = order;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Long getQuantity() {
return quantity;
}
public void setQuantity(Long quantity) {
this.quantity = quantity;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
@Override
public String toString() {
return "Orderline [id=" + id + ", price=" + price + ", quantity=" + quantity + ", product=" + product
+ "]";
}
}
Order.hmb.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.iotek.basic.association.one2many.pojo">
<class name="Order" table="T_ORDER">
<id name="id" column="ID" type="long">
<generator class="sequence">
<param name="sequence">t_order_seq</param>
</generator>
</id>
<property name="orderedDate" type="date" column="ORDERED_DATE"/>
<property name="shippedDate" type="date" column="SHIPPED_DATE"/>
<property name="total" type="double" column="TOTAL"/>
<set name="orderlines" cascade="all" inverse="true">
<key column="ORDER_ID"/>
<one-to-many class="Orderline"/>
</set>
</class>
</hibernate-mapping>
Orderline.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.iotek.basic.association.one2many.pojo">
<class name="Orderline" table="T_ORDERLINE">
<id name="id" column="ID" type="long">
<generator class="sequence">
<param name="sequence">t_orderline_seq</param>
</generator>
</id>
<property name="price" type="double" column="PRICE"/>
<property name="quantity" type="long" column="QUANTITY"/>
<property name="product" type="string" column="PRODUCT"/>
<many-to-one name="order" column="ORDER_ID"></many-to-one>
</class>
</hibernate-mapping>
hibernate配置文件:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- property 元素用于配置Hibernate中的属性 键:值 -->
<!-- hibernate.connection.driver_class : 连接数据库的驱动 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- hibernate.connection.url : 连接数据库的地址,路径 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<!-- hibernate.connection.username : 连接数据库的用户名 -->
<property name="connection.username">system</property>
<!-- hibernate.connection.password : 连接数据库的密码 -->
<property name="connection.password">root</property>
<!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->
<property name="show_sql">true</property>
<!-- 数据库方言配置 org.hibernate.dialect.Oracle10gDialect (选择最短的)-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 加载配置文件的pojo类,否则找不到,报错, unknow entity -->
<mapping resource="com/iotek/basic/pojo/Student.hbm.xml"/>
<mapping resource="com/iotek/basic/association/one2one/pojo/Passport_PK.hbm.xml"/>
<mapping resource="com/iotek/basic/association/one2one/pojo/Person_PK.hbm.xml"/>
<mapping resource="com/iotek/basic/association/one2many/pojo/Order.hbm.xml"/>
<mapping resource="com/iotek/basic/association/one2many/pojo/Orderline.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试类:One2ManyTest.java:
package com.iotek.basic.association.one2many.pojo;
import java.sql.Date;
import java.util.Calendar;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.iotek.basic.common.HibernateSessionFactory;
public class One2ManyTest {
public static void main(String[] args) {
Orderline orderline1 = new Orderline();
orderline1.setProduct("jdbc");
orderline1.setPrice(12.24);
orderline1.setQuantity(5L);
Orderline orderline2 = new Orderline();
orderline2.setProduct("java");
orderline2.setPrice(24.88);
orderline2.setQuantity(20L);
Order order = new Order();
order.setOrderedDate(new Date(System.currentTimeMillis()));
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH) + 3);
order.setShippedDate(new Date(c1.getTimeInMillis()));
order.getOrderlines().add(orderline1);
order.getOrderlines().add(orderline2);
orderline1.setOrder(order);
orderline2.setOrder(order);
order.setTotal(
orderline1.getPrice()*orderline1.getQuantity() +
orderline2.getPrice()*orderline2.getQuantity());
Session session = HibernateSessionFactory.getSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(order);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
}
}
}