1. 程式人生 > >JPA(五):映射關聯關系------映射單向多對一的關聯關系

JPA(五):映射關聯關系------映射單向多對一的關聯關系

span AS 127.0.0.1 mysql5 單向 cti isa 嘗試 sequence

映射單向多對一的關聯關系

新建Customer.java:

技術分享圖片
package com.dx.jpa.singlemanytoone;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
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; @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; } // 幫助方法,不希望保存到數據庫,但是需要動態獲取Customer對象的屬性。 @Transient public String getCustomerInfo() { return "username:" + fullName + ",age:" + age; } }
View Code

Order.java:

package com.dx.jpa.singlemanytoone;

import javax.persistence.Entity;
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來映射外鍵
    // @ManyToOne映射單項一對多映射關系
    @JoinColumn(name = "CUSTOMER_ID")
    @ManyToOne()
    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

}

Client.java測試類:

技術分享圖片
package com.dx.jpa.singlemanytoone;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.hamcrest.CustomTypeSafeMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Client {
    private String persistenceUnitName = "Jpa-helloword";
    private EntityManagerFactory entityManagerFactory = null;
    private EntityManager entityManager = null;
    private EntityTransaction entityTransaction = null;

    @Before
    public void init() {
        // 1.創建EntityManagerFactory
        entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
        // 2.創建EntityManager
        entityManager = entityManagerFactory.createEntityManager();
        // 3.開始事務
        entityTransaction = entityManager.getTransaction();
        entityTransaction.begin();
    }

    @After
    public void destory() {
        // 5.提交事務
        entityTransaction.commit();

        // 6.關閉EntityManager
        entityManager.close();
        // 7.關閉EnityManagerFactory
        entityManagerFactory.close();
    }
}
View Code

將Customer,Person添加到jpa配置文件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.dxsoft.jpa.helloword.Person</class>
        <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.MySQLDialect
            MySQL with InnoDB    org.hibernate.dialect.MySQLInnoDBDialect
            MySQL with MyISAM    org.hibernate.dialect.MySQLMyISAMDialect
            MySQL5                org.hibernate.dialect.MySQL5Dialect
            MySQL5 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>
View Code

執行初始化時,創建表打印語句如下:

Hibernate: 
    
    create table jpa_customer (
       id integer not null,
        age integer,
        birth date,
        createDate datetime,
        FULL_NAME varchar(64) not null,
        primary key (id)
    ) engine=InnoDB
Hibernate: 
    
    create table jpa_order (
       id integer not null,
        name varchar(255),
        CUSTOMER_ID integer,
        primary key (id)
    ) engine=InnoDB
Hibernate: 
    
    alter table jpa_order 
       add constraint FK7glkngwj74nr8h2amofkp1fjd 
       foreign key (CUSTOMER_ID) 
       references jpa_customer (id)

此時查看表,發現jpa_order表上新建了外鍵:

技術分享圖片

測試添加:

添加測試函數:

    @Test
    public 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");
        order1.setCustomer(customer);

        Order order2 = new Order();
        order2.setName("O-AA-02");
        order2.setCustomer(customer);

        entityManager.persist(customer);
        entityManager.persist(order1);
        entityManager.persist(order2);
    }

此時打印語句為:

技術分享圖片
Hibernate: 
    select
        next_val as id_val 
    from
        hibernate_sequence for update
            
Hibernate: 
    update
        hibernate_sequence 
    set
        next_val= ? 
    where
        next_val=?
Hibernate: 
    select
        next_val as id_val 
    from
        hibernate_sequence for update
            
Hibernate: 
    update
        hibernate_sequence 
    set
        next_val= ? 
    where
        next_val=?
Hibernate: 
    select
        next_val as id_val 
    from
        hibernate_sequence for update
            
Hibernate: 
    update
        hibernate_sequence 
    set
        next_val= ? 
    where
        next_val=?
Hibernate: 
    insert 
    into
        jpa_customer
        (age, birth, createDate, FULL_NAME, id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        jpa_order
        (CUSTOMER_ID, name, id) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        jpa_order
        (CUSTOMER_ID, name, id) 
    values
        (?, ?, ?)
View Code

修改customer,order1,order2添加順序:

    @Test
    public void testPersist() {
        Customer customer = new Customer();
        customer.setFullName("BB");
        customer.setAge(26);
        customer.setBirth(new Date());
        customer.setCreateDate(new Date());

        Order order1 = new Order();
        order1.setName("O-BB-01");
        order1.setCustomer(customer);

        Order order2 = new Order();
        order2.setName("O-BB-02");
        order2.setCustomer(customer);

        entityManager.persist(order1);
        entityManager.persist(order2);
        entityManager.persist(customer);
    }

執行打印sql語句如下:

技術分享圖片
Hibernate: 
    select
        next_val as id_val 
    from
        hibernate_sequence for update
            
Hibernate: 
    update
        hibernate_sequence 
    set
        next_val= ? 
    where
        next_val=?
Hibernate: 
    select
        next_val as id_val 
    from
        hibernate_sequence for update
            
Hibernate: 
    update
        hibernate_sequence 
    set
        next_val= ? 
    where
        next_val=?
Hibernate: 
    select
        next_val as id_val 
    from
        hibernate_sequence for update
            
Hibernate: 
    update
        hibernate_sequence 
    set
        next_val= ? 
    where
        next_val=?
Hibernate: 
    insert 
    into
        jpa_order
        (CUSTOMER_ID, name, id) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        jpa_order
        (CUSTOMER_ID, name, id) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        jpa_customer
        (age, birth, createDate, FULL_NAME, id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    update
        jpa_order 
    set
        CUSTOMER_ID=?,
        name=? 
    where
        id=?
Hibernate: 
    update
        jpa_order 
    set
        CUSTOMER_ID=?,
        name=? 
    where
        id=?
View Code

從打印信息中可以看出,此時是先插入的order,之後在修改其customer_id。比起之前的那種寫法多出來了兩次修改。

測試查詢:

添加測試查詢函數:

    @Test
    public void testSelect() {
        Order order = entityManager.find(Order.class, 3);
        System.out.println(order.getName());
        //System.out.println(order.getCustomer().getFullName());
    }

此時執行打印sql語句為:

Hibernate: 
    select
        order0_.id as id1_1_0_,
        order0_.CUSTOMER_ID as CUSTOMER3_1_0_,
        order0_.name as name2_1_0_,
        customer1_.id as id1_0_1_,
        customer1_.age as age2_0_1_,
        customer1_.birth as birth3_0_1_,
        customer1_.createDate as createDa4_0_1_,
        customer1_.FULL_NAME as FULL_NAM5_0_1_ 
    from
        jpa_order order0_ 
    left outer join
        jpa_customer customer1_ 
            on order0_.CUSTOMER_ID=customer1_.id 
    where
        order0_.id=?

從sql語句可以看出即使不插敘customer信息,也會使用left outer join把customer信息給關聯出來。

修改Order的getCustomer方法的屬性:

技術分享圖片

修改Order屬性為:

技術分享圖片

重新測試查詢:

    @Test
    public void testSelect() {
        Order order = entityManager.find(Order.class, 2);
        System.out.println("------------------------");
        System.out.println(order.getName());
        System.out.println("------------------------");
        System.out.println(order.getCustomer().getFullName());
        System.out.println("------------------------");
    }

打印sql語句為:

Hibernate: 
    select
        order0_.id as id1_1_0_,
        order0_.CUSTOMER_ID as CUSTOMER3_1_0_,
        order0_.name as name2_1_0_ 
    from
        jpa_order order0_ 
    where
        order0_.id=?
------------------------
O-BB-02
------------------------
Hibernate: 
    select
        customer0_.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_ 
    from
        jpa_customer customer0_ 
    where
        customer0_.id=?
BB
------------------------

查詢修改:

添加修改測試:

    @Test
    public void testUpdate() {
        Order order = entityManager.find(Order.class, 1);
        order.setName("O-BBB-01");
        order.getCustomer().setFullName("BBB");
    }

打印執行sql:

技術分享圖片
Hibernate: 
    select
        order0_.id as id1_1_0_,
        order0_.CUSTOMER_ID as CUSTOMER3_1_0_,
        order0_.name as name2_1_0_ 
    from
        jpa_order order0_ 
    where
        order0_.id=?
Hibernate: 
    select
        customer0_.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_ 
    from
        jpa_customer customer0_ 
    where
        customer0_.id=?
Hibernate: 
    update
        jpa_order 
    set
        CUSTOMER_ID=?,
        name=? 
    where
        id=?
Hibernate: 
    update
        jpa_customer 
    set
        age=?,
        birth=?,
        createDate=?,
        FULL_NAME=? 
    where
        id=?
View Code

查詢刪除:

測試刪除多的一端:

    @Test
    public void testRemove() {
        Order order = entityManager.find(Order.class, 1);
        entityManager.remove(order);        
    }
    

執行打印語句:

Hibernate: 
    select
        order0_.id as id1_1_0_,
        order0_.CUSTOMER_ID as CUSTOMER3_1_0_,
        order0_.name as name2_1_0_ 
    from
        jpa_order order0_ 
    where
        order0_.id=?
Hibernate: 
    delete 
    from
        jpa_order 
    where
        id=?

刪除一的一端,此時其在多的一端還對應的有數據記錄:

技術分享圖片

技術分享圖片

執行測試函數:

    @Test
    public void testRemove() {
        Customer customer = entityManager.find(Customer.class, 3);
        entityManager.remove(customer);
    }

提示刪除失敗:

技術分享圖片

再次修改,將多一端的對應的數據也刪除後,再次嘗試刪除:

    @Test
    public void testRemove() {
        Order order = entityManager.find(Order.class, 2);
        entityManager.remove(order);

        Customer customer = entityManager.find(Customer.class, 3);
        entityManager.remove(customer);
    }

此時提示刪除成功。

JPA(五):映射關聯關系------映射單向多對一的關聯關系