JPA笔记2 OneToMany
作者:互联网
package one_to_many; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; 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.OneToMany; import javax.persistence.Table; @Table(name = "JPA_CUSTOMERS") @Entity public class Customer2 { @Override public String toString() { return "Customer [id=" + id + ", lastName=" + lastName + ", email=" + email + ", age=" + age + ", cla=" + cla + ", ff=" + ff + "]"; } private Integer id; private String lastName; private String email; private int age; private int cla; private String ff; private Set<Order2> orders = new HashSet<>(); @GeneratedValue(strategy = GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getCla() { return cla; } public void setCla(int cla) { this.cla = cla; } public String getFf() { return ff; } public void setFf(String ff) { this.ff = ff; } // 删除1的一端,默认会先修改多的一端,然后再删除1的一端 // 可以通过修改@OneToMany的cascade属性来修改默认的删除策略 @JoinColumn(name = "customer_id") @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.REMOVE }) public Set<Order2> getOrders() { return orders; } public void setOrders(Set<Order2> orders) { this.orders = orders; } }
package one_to_many; import javax.persistence.Column; import javax.persistence.ConstraintMode; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.ForeignKey; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Table(name = "JPA_ORDER") @Entity public class Order2 { private Integer id; private String orderName; // private Customer customer; @GeneratedValue(strategy = GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "order_name") public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } // //映射单向n-1的关联关系 // @JoinColumn(name = "customer_id")//, foreignKey = // @ForeignKey(ConstraintMode.NO_CONSTRAINT)) // @ManyToOne(fetch=FetchType.LAZY) // public Customer getCustomer() { // return customer; // } // // public void setCustomer(Customer customer) { // this.customer = customer; // } @Override public String toString() { return "Order [id=" + id + ", orderName=" + orderName + "]"; } }
package one_to_many; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class Main2 { public static void main(String[] args) { // TODO Auto-generated method stub String persistenceUnitName = "jpa-1"; EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // oneToManyPersist(entityManager); // oneToManyRemove(entityManager); oneToManyUpdate(entityManager); transaction.commit(); entityManager.close(); entityManagerFactory.close(); } // 1.单向1-n关联关系执行保存时,一定会多出update语句 // 因为n的一端在插入时不会同时插入外键 private static void oneToManyPersist(EntityManager entityManager) { Customer2 customer = new Customer2(); customer.setEmail("fs2@ss.com"); customer.setLastName("123"); customer.setAge(23); customer.setCla(33); customer.setFf("aa"); Order2 order2 = new Order2(); order2.setOrderName("1"); Order2 order3 = new Order2(); order3.setOrderName("2"); customer.getOrders().add(order2); customer.getOrders().add(order3); entityManager.persist(order2); entityManager.persist(order3); entityManager.persist(customer); } // 默认对关联的多的一方做懒加载 private static void oneToManyFind(EntityManager entityManager) { Customer2 customer2 = entityManager.find(Customer2.class, 5); System.out.println(customer2); System.out.println(customer2.getOrders().size()); } // 删除1的一端,默认会先修改多的一端,然后再删除1的一端 // 可以通过修改@OneToMany的cascade属性来修改默认的删除策略 private static void oneToManyRemove(EntityManager entityManager) { Customer2 customer2 = entityManager.find(Customer2.class, 5); entityManager.remove(customer2); } private static void oneToManyUpdate(EntityManager entityManager) { Customer2 customer2 = entityManager.find(Customer2.class, 6); customer2.getOrders().iterator().next().setOrderName("xx"); } }
标签:entityManager,String,OneToMany,JPA,笔记,public,import,javax,persistence 来源: https://www.cnblogs.com/zhuawang/p/11558719.html