映射双向多对一的关联关系
修改Customer.java
package com.dx.jpa.singlemanytoone;import java.util.Date; 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.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient;@Entity @Table(name = "jpa_customer") public class Customer {private Integer id;private String fullName;private Integer age;private Date birth;private Date createDate;private Set<Order> orders = new HashSet<>();@Id@GeneratedValue(strategy = GenerationType.AUTO)public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Column(name = "FULL_NAME", length = 64, nullable = false)public String getFullName() {return fullName;}public void setFullName(String fullName) {this.fullName = fullName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Temporal(TemporalType.DATE)public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}@Temporal(TemporalType.TIMESTAMP)public Date getCreateDate() {return createDate;}public void setCreateDate(Date createDate) {this.createDate = createDate;}// 映射一对多的关联关系// @JoinColumn 用来映射一对多的关联关系// @OneToMany 用来映射外键列@JoinColumn(name = "CUSTOMER_ID")@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.REMOVE)public Set<Order> getOrders() {return orders;}public void setOrders(Set<Order> orders) {this.orders = orders;}// 帮助方法,不希望保存到数据库,但是需要动态获取Customer对象的属性。 @Transientpublic String getCustomerInfo() {return "username:" + fullName + ",age:" + age;} }
修改Order.java
package com.dx.jpa.singlemanytoone;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; import javax.persistence.Table;@Entity @Table(name = "jpa_order") public class Order {private Integer id;private String name;private Customer Customer;@Id@GeneratedValue(strategy = GenerationType.AUTO)public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@JoinColumn(name = "CUSTOMER_ID")@ManyToOne(fetch = FetchType.LAZY)public Customer getCustomer() {return Customer;}public void setCustomer(Customer customer) {Customer = customer;} }
修改persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0"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_2_0.xsd"><persistence-unit name="Jpa-helloword"transaction-type="RESOURCE_LOCAL"><!-- 配置使用什么 ORM 产品来作为 JPA 的实现 --><provider>org.hibernate.jpa.HibernatePersistenceProvider</provider><!-- 添加持久化类 --><class>com.dx.jpa.singlemanytoone.Customer</class><class>com.dx.jpa.singlemanytoone.Order</class><properties><!-- 数据库的相关配置 --><property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /><property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/jpa" /><property name="javax.persistence.jdbc.user" value="root" /><property name="javax.persistence.jdbc.password" value="root" /><!-- 指定方言 MySQL org.hibernate.dialect.MySQLDialectMySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialectMySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialectMySQL5 org.hibernate.dialect.MySQL5DialectMySQL5 with InnoDB org.hibernate.dialect.MySQL5InnoDBDialect--><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /><property name="hibernate.show_sql" value="true" /><property name="hibernate.format_sql" value="true" /><!-- create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。<br>create-drop :每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。<br>update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。<br>validate :每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。 <br> --><property name="hibernate.hbm2ddl.auto" value="update" /> </properties></persistence-unit> </persistence>
特别注意:配置双向多对一的关联关系时,Customer.java配置:
// 映射一对多的关联关系// @JoinColumn 用来映射一对多的关联关系// @OneToMany 用来映射外键列@JoinColumn(name = "CUSTOMER_ID")@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.REMOVE)public Set<Order> getOrders() {return orders;}
Order.java配置:
@JoinColumn(name = "CUSTOMER_ID")@ManyToOne(fetch = FetchType.LAZY)public Customer getCustomer() {return Customer;}
表关联关系:
测试添加:
添加测试函数:
@Testpublic void testPersist() {Customer customer = new Customer();customer.setFullName("AA");customer.setAge(26);customer.setBirth(new Date());customer.setCreateDate(new Date());Order order1 = new Order();order1.setName("O-AA-01");Order order2 = new Order();order2.setName("O-AA-02");customer.getOrders().add(order1);customer.getOrders().add(order2);order1.setCustomer(customer);order2.setCustomer(customer);entityManager.persist(customer);entityManager.persist(order1);entityManager.persist(order2);}
执行sql::
Hibernate: selectnext_val as id_val fromhibernate_sequence for updateHibernate: updatehibernate_sequence setnext_val= ? wherenext_val=? Hibernate: selectnext_val as id_val fromhibernate_sequence for updateHibernate: updatehibernate_sequence setnext_val= ? wherenext_val=? Hibernate: selectnext_val as id_val fromhibernate_sequence for updateHibernate: updatehibernate_sequence setnext_val= ? wherenext_val=? Hibernate: insert intojpa_customer(age, birth, createDate, FULL_NAME, id) values(?, ?, ?, ?, ?) Hibernate: insert intojpa_order(CUSTOMER_ID, name, id) values(?, ?, ?) Hibernate: insert intojpa_order(CUSTOMER_ID, name, id) values(?, ?, ?) Hibernate: updatejpa_order setCUSTOMER_ID=? whereid=? Hibernate: updatejpa_order setCUSTOMER_ID=? whereid=?
测试查询:
测试查询函数:
@Testpublic void testFind() {Customer customer = entityManager.find(Customer.class, 1);System.out.println(customer.getFullName());System.out.println(customer.getOrders().size());}
执行sql语句:
Hibernate: selectcustomer0_.id as id1_0_0_,customer0_.age as age2_0_0_,customer0_.birth as birth3_0_0_,customer0_.createDate as createDa4_0_0_,customer0_.FULL_NAME as FULL_NAM5_0_0_,orders1_.CUSTOMER_ID as CUSTOMER3_1_1_,orders1_.id as id1_1_1_,orders1_.id as id1_1_2_,orders1_.CUSTOMER_ID as CUSTOMER3_1_2_,orders1_.name as name2_1_2_ fromjpa_customer customer0_ left outer joinjpa_order orders1_ on customer0_.id=orders1_.CUSTOMER_ID wherecustomer0_.id=? BB 2
测试修改:
测试修改函数:
执行sql语句:
测试删除:
4)映射双向一对一的关联关系
5)映射双向多对多的关联关系