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,
mappedBy= "order" )
private Set<OrderItem> orderItems = new
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this .orderId
}
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this .amount
}
public Set<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(Set<OrderItem> orderItems) {
this .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 , false )
private String productName;
@Column (nullable= false )
private Float sellPrice;
@ManyToOne (cascade={CascadeType.REFRESH, false )
@JoinColumn (name= "item_order_for" )
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this .order
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this .productName
}
public Float getSellPrice() {
return sellPrice;
}
public void setSellPrice(Float sellPrice) {
this .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
.createEntityManagerFactory( "unitname" );
EntityManager entityManager = entityManagerFactory
.createEntityManager();
entityManager.getTransaction().begin();
Order order = new
order.setOrderId( "jfdkf323" );
order.setAmount(232f);
OrderItem orderItem = new
orderItem.setProductName( "basketball" );
orderItem.setSellPrice(221f);
OrderItem orderItem2 = new
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(一)、、、一样,在此省略