JPA 一对多实例配置
程序员文章站
2022-03-03 09:41:11
...
http://zmx.iteye.com/blog/487063
- <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
- version="1.0">
- <persistence-unit name="mengya" transaction-type="RESOURCE_LOCAL">
- <properties>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
- <property name="hibernate.hbm2ddl.auto" value="update"/>
- <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
- <property name="hibernate.connection.username" value="root"/>
- <property name="hibernate.connection.password" value="123"/>
- <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=UTF-8"/>
- </properties>
- </persistence-unit>
- </persistence>
在JPA中双向一对多关系则多方为关系护端即负责外键记录的更新,一方为被维护端即没有权力更新外键记录
src下在面的META-INF/persistence.xml配置如下:
订单和订单项两个实例实体Bean如下:
订单表:
- package com.mengya.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.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- @Entity
- @Table(name = "orders")
- public class Order {
- private String orderId;
- private Float amount = 0f;// 设置默认值
- private Set<OrderItem> items = new HashSet<OrderItem>();
- @Id
- @Column(length = 32)
- public String getOrderId() {
- return orderId;
- }
- public void setOrderId(String orderId) {
- this.orderId = orderId;
- }
- @Column(nullable = false)
- public Float getAmount() {
- return amount;
- }
- public void setAmount(Float amount) {
- this.amount = amount;
- }
- // cascade表示级联操作
- // CascadeType.MERGE级联更新:若items属性修改了那么order对象保存时同时修改items里的对象。对应EntityManager的merge方法
- // CascadeType.PERSIST级联刷新:获取order对象里也同时也重新获取最新的items时的对象。对应EntityManager的refresh(object)方法有效。即会重新查询数据库里的最新数据
- // CascadeType.REFRESH级联保存:对order对象保存时也对items里的对象也会保存。对应EntityManager的presist方法
- // CascadeType.REMOVE级联删除:对order对象删除也对items里的对象也会删除。对应EntityManager的remove方法
- // FetchType.LAZY表示懒加载。对于xxxtoMany时即获得多的一方fetch的默认值是FetchType.LAZY懒加载。对于xxxtoOne时即获得一的一方fetch的默认值是FetchType.EAGER立即加载
- // mappedBy表示关系统被维护端,它的值是关系维护端维护关系的属性
- @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
- CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "order")
- public Set<OrderItem> getItems() {
- return items;
- }
- public void setItems(Set<OrderItem> items) {
- this.items = items;
- }
- /**
- * 自己的加的一个方法,为了方便添加OrderItem
- *
- */
- public void addOrderItem(OrderItem item) {
- item.setOrder(this);
- this.items.add(item);
- }
- }
订单项表:
- package com.mengya.bean;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- @Entity
- public class OrderItem {
- private Integer id;
- private String productName;
- private Float sellPrice = 0f;// 设置默认值
- private Order order;
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- @Column(length = 40, nullable = false)
- public String getProductName() {
- return productName;
- }
- public void setProductName(String productName) {
- this.productName = productName;
- }
- @Column(nullable = false)
- public Float getSellPrice() {
- return sellPrice;
- }
- public void setSellPrice(Float sellPrice) {
- this.sellPrice = sellPrice;
- }
- // cascade表示级联。CascadeType.REFRESH级联刷新
- // optional表示该对象可有可无,它的值为true表示该外键可以为null,它的值为false表示该外键为not null
- @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, optional = false)
- // JoinColumn表示外键的列
- @JoinColumn(name="orderId")
- public Order getOrder() {
- return order;
- }
- public void setOrder(Order order) {
- this.order = order;
- }
- }
测试:
- package com.mengya.junit.test;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import org.junit.Test;
- import com.mengya.bean.Order;
- import com.mengya.bean.OrderItem;
- public class JPA_OnetoManyTest {
- @Test public void save(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("mengya");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- Order order=new Order();
- order.setOrderId("200910120001");
- order.setAmount(450f);
- OrderItem item1=new OrderItem();
- item1.setProductName("篮球");
- item1.setSellPrice(220f);
- OrderItem item2=new OrderItem();
- item2.setProductName("排球");
- item2.setSellPrice(230f);
- order.addOrderItem(item1);
- order.addOrderItem(item2);
- em.persist(order);
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- }