标签:
Hibernate之双向一对多(三)
一.简介
本篇博文接着上一章的内容接着开展,代码也是在上篇博文的基础上修改。
二.hbm文件的方式
Customer.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> <class name="com.demo.hibernate.one2many.Customer" table="customer"> <id name="id" type="int"> <generator class="sequence"> <param name="sequence_name">cus_order_seq</param> </generator> </id> <property name="name" type="string" column="name"></property> <property name="phoneNum" type="string" column="phone_number"></property> <!-- inverse表示由对方维护这种关联关系 --> <set name="orderSet" inverse="true"> <key column="customer_id"></key> <one-to-many class="com.demo.hibernate.one2many.Order"/> </set> </class> </hibernate-mapping>
Order.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> <class name="com.demo.hibernate.one2many.Order" table="orders"> <id name="id" type="int"> <generator class="sequence"> <param name="sequence_name">cus_order_seq</param> </generator> </id> <property name="orderId" column="order_id" type="string"></property> <property name="createTime" column="create_time" type="timestamp"></property> <many-to-one name="customer" class="com.demo.hibernate.one2many.Customer" column="customer_id"></many-to-one> </class> </hibernate-mapping>
三.注解的方式
Customer.java
@Entity @Table(name="customer") public class Customer { @Id @Column(name="id") @SequenceGenerator(name="sequenceGenerator", sequenceName="cus_order_seq", allocationSize=1) @GeneratedValue(generator="sequenceGenerator", strategy=GenerationType.SEQUENCE) private int id; @Column(name="name") private String name; @Column(name="phone_number") private String phoneNum; @OneToMany(fetch=FetchType.LAZY, mappedBy="customer") private Set<Order> orderSet; //setter and getter }
Order.java
@Entity @Table(name="orders") public class Order { @Id @Column(name="id") @SequenceGenerator(name="sequenceGenerator", sequenceName="cus_order_seq", allocationSize=1) @GeneratedValue(generator="sequenceGenerator", strategy=GenerationType.SEQUENCE) private int id; @Column(name="order_id") private String orderId; @Column(name="create_time") @Type(type="timestamp") private Date createTime; @ManyToOne(fetch=FetchType.LAZY) private Customer customer; //setter and getter }
四.测试代码
A.保存
@Test public void save(){ Transaction tx = session.beginTransaction(); Customer customer = new Customer(); customer.setName("AAAAA"); customer.setPhoneNum("334411"); Order order = new Order(); order.setCreateTime(new Date()); order.setOrderId("A"); Order order1 = new Order(); order1.setCreateTime(new Date()); order1.setOrderId("B"); Set<Order> orderSet = new HashSet<Order>(); orderSet.add(order); orderSet.add(order1); session.save(customer); session.save(order); session.save(order1); tx.commit();
}
标签:
原文地址:http://www.cnblogs.com/miller-zou/p/5727684.html