JPA(三)之实体关系一对多(多对一)


1.背景介绍:

   对于购买商品时,订单信息(Order)和订单商品信息(OrderItem)的关系就是一对多的关系。

 

2.实体bean:

  Order.java代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.sinoi.bean;
 
import java.util.HashSet;
import java.util.Set;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
 
@Entity
@Table(name="my_order")
public class Order {
    @Id @Column(length=12)
    private String orderId;
    @Column(nullable=false)
    private Float amount = 0f;
    @OneToMany(cascade={CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH},
            mappedBy="order")
    private Set<OrderItem> orderItems = new HashSet<OrderItem>();
    public String getOrderId() {
        return orderId;
    }
    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
    public Float getAmount() {
        return amount;
    }
    public void setAmount(Float amount) {
        this.amount = amount;
    }
     
    public Set<OrderItem> getOrderItems() {
        return orderItems;
    }
    public void setOrderItems(Set<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }
    public void addOrderItem(OrderItem orderItem) {
        orderItem.setOrder(this);//这句代码不加也可!
        this.orderItems.add(orderItem);
    }
     
     
}

OrderItem.java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.sinoi.bean;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
 
@Entity
public class OrderItem {
    @Id @GeneratedValue
    private Integer id;
    @Column(length=20, nullable=false)
    private String productName;
    @Column(nullable=false)
    private Float sellPrice;
    @ManyToOne(cascade={CascadeType.REFRESH, CascadeType.MERGE}, optional=false)
    @JoinColumn(name="item_order_for")
    private Order order;
    public Order getOrder() {
        return order;
    }
    public void setOrder(Order order) {
        this.order = order;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getProductName() {
        return productName;
    }
    public void setProductName(String productName) {
        this.productName = productName;
    }
    public Float getSellPrice() {
        return sellPrice;
    }
    public void setSellPrice(Float sellPrice) {
        this.sellPrice = sellPrice;
    }
}

 

3.测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package junit.test;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
import org.junit.Test;
 
import com.sinoi.bean.Order;
import com.sinoi.bean.OrderItem;
 
public class UnitTest {
    @Test
    public void save() {
        EntityManagerFactory entityManagerFactory = Persistence
                .createEntityManagerFactory("unitname");
        EntityManager entityManager = entityManagerFactory
                .createEntityManager();
        entityManager.getTransaction().begin();
         
        Order order = new Order();
        order.setOrderId("jfdkf323");
        order.setAmount(232f);
        OrderItem orderItem = new OrderItem();
        orderItem.setProductName("basketball");
        orderItem.setSellPrice(221f);
        OrderItem orderItem2 = new OrderItem();
        orderItem2.setProductName("football");
        orderItem2.setSellPrice(11f);
        orderItem.setOrder(order);
        orderItem2.setOrder(order);
        order.addOrderItem(orderItem);
        order.addOrderItem(orderItem2);
 
        entityManager.persist(order);
        entityManager.getTransaction().commit();
        entityManager.close();
        entityManagerFactory.close();
         
    }
}

 

 

4.配置文件persistence.xml和前面JPA(一)、、、一样,在此省略

上一篇:Tomcat配置域名访问


下一篇:[Fluent NHibernate]一对多关系处理