JPA实现一对多
程序员文章站
2022-04-23 15:45:50
...
[color=blue]JPA实现一对多,以定单和定单项为例[/color]
[color=red]1.Order.java 定单类[/color]
[color=red]2.OrderItem.java 定单项类[/color]
[color=red]3.One2ManyTest.java 测试类[/color]
[color=red]1.Order.java 定单类[/color]
package com.cyberwise.jpa.one2many;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
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;
import javax.persistence.Version;
@Entity
@Table(name = "orders")
public class Order implements Serializable {
@Version
private Long version;
@Id
private String orderId;
private Float amount = 0f;
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
CascadeType.REMOVE, CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "order")//这里配了mappedBy,也就是属于双向关联了,既可以从这张表找到另外一张表,也可以从别外的表找到这张表
private List<OrderItem> items = new ArrayList<OrderItem>();
public Order(){
}
@Column(name="版本号")
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
@Column(name="定单号",length = 12)
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
@Column(name="总数",nullable = false)
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
public List<OrderItem> getItems() {
return items;
}
public void setItems(List<OrderItem> items) {
this.items = items;
}
public void addOrderItem(OrderItem orderItem){
orderItem.setOrder(this);
this.items.add(orderItem);
}
}
[color=red]2.OrderItem.java 定单项类[/color]
package com.cyberwise.jpa.one2many;
import java.io.Serializable;
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;
import javax.persistence.Table;
import javax.persistence.Version;
@Entity
@Table(name="orderitems")
public class OrderItem implements Serializable{
@Version
private Long version;
@Id
@GeneratedValue
private Integer id;
private String productName;
private Float sellPrice =0f;//默认为0
@ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
@JoinColumn(name="order_id")//设置外键的名称
private Order order;
public OrderItem(){
}
@Column(name="版本号")
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="产品名称",length=40,nullable=false)
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@Column(name="产品价格",nullable=false)
public Float getSellPrice() {
return sellPrice;
}
public void setSellPrice(Float sellPrice) {
this.sellPrice = sellPrice;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
[color=red]3.One2ManyTest.java 测试类[/color]
package com.cyberwise.jpa.one2many;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.jboss.seam.annotations.In;
public class One2ManyTest {
@In
EntityManagerFactory factory;
@In
EntityManager manager;
public void save(){
factory = Persistence.createEntityManagerFactory("test");
manager = factory.createEntityManager();
manager.getTransaction().begin();
Order order = new Order();
order.setAmount(200f);
order.setOrderId("9002");
OrderItem orderItem = new OrderItem();
orderItem.setProductName("跳绳");
orderItem.setSellPrice(15f);
OrderItem orderItem2 = new OrderItem();
orderItem2.setProductName("橄榄球");
orderItem2.setSellPrice(200f);
order.addOrderItem(orderItem);
order.addOrderItem(orderItem2);
manager.persist(order);
manager.getTransaction().commit();
manager.close();
factory.close();
}
/**查看产品*/
public void query(){
factory = Persistence.createEntityManagerFactory("test");
manager = factory.createEntityManager();
String sql = "select o from OrderItem o where o.order='9002'";
List<OrderItem> productList = manager.createQuery(sql).getResultList();
System.out.println("产品名称:");
for(OrderItem o : productList){
System.out.println(o.getProductName());
}
manager.close();
factory.close();
}
/**这是修改定单表*/
public void update(){
factory = Persistence.createEntityManagerFactory("test");
manager = factory.createEntityManager();
manager.getTransaction().begin();
String ql ="update Order o set o.amount =:amount where o.orderId=:orderId";
Query query = manager.createQuery(ql);
query.setParameter("amount",100f );
query.setParameter("orderId", "9002");
query.executeUpdate();
manager.getTransaction().commit();
manager.close();
factory.close();
}
/**修改定单项*/
public void updateItem(){
factory = Persistence.createEntityManagerFactory("test");
manager = factory.createEntityManager();
manager.getTransaction().begin();
String sql = "update OrderItem o set o.order.orderId =:orderId where o.id=:id";
Query query = manager.createQuery(sql);
query.setParameter("orderId", "9001");
query.setParameter("id", 3);
query.executeUpdate();
manager.getTransaction().commit();
manager.close();
factory.close();
}
/**增加定单项*/
public void addProduct(){
factory = Persistence.createEntityManagerFactory("test");
manager = factory.createEntityManager();
manager.getTransaction().begin();
//增加定单号为9002的定单
String id = "9002";
Order order1 = manager.find(Order.class,id);
OrderItem orderItem = new OrderItem();
orderItem.setOrder(order1);
orderItem.setProductName("橄榄球");
orderItem.setSellPrice(115f);
manager.persist(orderItem);
manager.getTransaction().commit();
manager.close();
factory.close();
}
public static void main(String[] args) {
One2ManyTest test = new One2ManyTest();
// test.save();
test.query();
// test.update();
// test.updateItem();
// test.addProduct();
}
}
上一篇: 看看同一种字体是如何对应不同的字体文件的