JPA--SpringDataJPA对象导航查询及加载策略

对象导航查询

对象图导航检索方式是根据已经加载的对象,导航到他的关联对象。它利用类与类之间的关系来检索对象。
例如:我们通过ID查询方式查出一个客户,可以调用Customer类中的getLinkMans()方法来获取该客户的所有联系人。对象导航查询的使用要求是:两个对象之间必须存在关联关系。

准备表和数据

在这里插入图片描述在这里插入图片描述

实体类

package pers.zhang.entity;

/**
 * @author zhang
 * @date 2019/12/15 - 22:10
 */

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 *  1.实体类和表的映射关系
 *      @Entity
 *      @Table
 *  2.类中属性和表中字段的映射关系
 *      @Id
 *      @GeneratedValue
 *      @Colum
 */

@Entity //声明实体类
@Table(name="cst_customer") //建立实体类和表的映射关系
public class Customer {

    @Id//声明当前私有属性为主键
    @GeneratedValue(strategy= GenerationType.IDENTITY) //配置主键的生成策略
    @Column(name="cust_id") //指定和表中cust_id字段的映射关系
    private Long custId;

    @Column(name="cust_name") //指定和表中cust_name字段的映射关系
    private String custName;

    @Column(name="cust_source")//指定和表中cust_source字段的映射关系
    private String custSource;

    @Column(name="cust_industry")//指定和表中cust_industry字段的映射关系
    private String custIndustry;

    @Column(name="cust_level")//指定和表中cust_level字段的映射关系
    private String custLevel;

    @Column(name="cust_address")//指定和表中cust_address字段的映射关系
    private String custAddress;

    @Column(name="cust_phone")//指定和表中cust_phone字段的映射关系
    private String custPhone;

    //配置客户和联系人之间的关系(一对多)
    /*
        1.声明关系:
            配置一对多关系:@OneToMany:targetEntity(对方对象的字节码)
        2.配置外键(中间表)
            name:外键字段的名称
            referencedColumnName:参照的主表的主键字段名称

        在客户实体类上(一的一方)添加了外键的配置,所以对于客户而言,也具备了维护外键的作用
        实际情况下,只需要一方维护关系即可
            mappedBy表示:放弃外键维护,由对方维护,取值为对方配置关系的属性名称


        级联:cascade
            CascadeType.ALL:所有
                        MERGE:更新
                        PERSIST:保存
                        REMOVE:删除

        fetch:配置关联对象的加载方式
            EAGER:立即加载
            LAZY:延迟加载

     */
//    @OneToMany(targetEntity = LinkMan.class)
//    @JoinColumn(name = "lkm_cust_id", referencedColumnName = "cust_id")
    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<LinkMan> linkMans = new HashSet<>();

    public Set<LinkMan> getLinkMans() {
        return linkMans;
    }

    public void setLinkMans(Set<LinkMan> linkMans) {
        this.linkMans = linkMans;
    }

    public Long getCustId() {
        return custId;
    }
    public void setCustId(Long custId) {
        this.custId = custId;
    }
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    public String getCustSource() {
        return custSource;
    }
    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }
    public String getCustIndustry() {
        return custIndustry;
    }
    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }
    public String getCustLevel() {
        return custLevel;
    }
    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }
    public String getCustAddress() {
        return custAddress;
    }
    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }
    public String getCustPhone() {
        return custPhone;
    }
    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "custId=" + custId +
                ", custName='" + custName + '\'' +
                ", custSource='" + custSource + '\'' +
                ", custIndustry='" + custIndustry + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custAddress='" + custAddress + '\'' +
                ", custPhone='" + custPhone + '\'' +
                '}';
    }
}
package pers.zhang.entity;

import org.springframework.test.context.ContextConfiguration;
import sun.awt.SunHints;

import javax.naming.Name;
import javax.persistence.*;

/**
 * @author zhang
 * @date 2019/12/17 - 20:23
 */
@Entity
@Table(name = "cst_linkman")
public class LinkMan {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "lkm_id")
    private Long lkmId;//联系人编号

    @Column(name = "lkm_name")
    private String lkmName;//联系人姓名

    @Column(name = "lkm_gender")
    private String lkmGender;//联系人性别

    @Column(name = "lkm_phone")
    private String lkmPhone;//联系人办公电话

    @Column(name = "lkm_mobile")
    private String lkmMobile;//联系人手机

    @Column(name = "lkm_email")
    private String lkmEmail;//联系人E-mail

    @Column(name = "lkm_position")
    private String lkmPosition;//联系人地址

    @Column(name = "lkm_memo")
    private String lkmMemo;//联系人备注

    //配置联系人到客户的多对一关系
    /*
        @ManyToOne:配置多对一关系
            targetEntity:对方的实体类额字节码
        @JoinColumn:配置外键(中间表)
            name:外键字段名
            referencedColumnName:外键引用的主表主键字段名
     */
    @ManyToOne(targetEntity = Customer.class)
    @JoinColumn(name = "lkm_cust_id", referencedColumnName = "cust_id")
    private Customer customer;

    public Customer getCustomer() {
        return customer;
    }

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

    public Long getLkmId() {
        return lkmId;
    }

    public void setLkmId(Long lkmId) {
        this.lkmId = lkmId;
    }

    public String getLkmName() {
        return lkmName;
    }

    public void setLkmName(String lkmName) {
        this.lkmName = lkmName;
    }

    public String getLkmGender() {
        return lkmGender;
    }

    public void setLkmGender(String lkmGender) {
        this.lkmGender = lkmGender;
    }

    public String getLkmPhone() {
        return lkmPhone;
    }

    public void setLkmPhone(String lkmPhone) {
        this.lkmPhone = lkmPhone;
    }

    public String getLkmMobile() {
        return lkmMobile;
    }

    public void setLkmMobile(String lkmMobile) {
        this.lkmMobile = lkmMobile;
    }

    public String getLkmEmail() {
        return lkmEmail;
    }

    public void setLkmEmail(String lkmEmail) {
        this.lkmEmail = lkmEmail;
    }

    public String getLkmPosition() {
        return lkmPosition;
    }

    public void setLkmPosition(String lkmPosition) {
        this.lkmPosition = lkmPosition;
    }

    public String getLkmMemo() {
        return lkmMemo;
    }

    public void setLkmMemo(String lkmMemo) {
        this.lkmMemo = lkmMemo;
    }


    @Override
    public String toString() {
        return "LinkMan{" +
                "lkmId=" + lkmId +
                ", lkmName='" + lkmName + '\'' +
                ", lkmGender='" + lkmGender + '\'' +
                ", lkmPhone='" + lkmPhone + '\'' +
                ", lkmMobile='" + lkmMobile + '\'' +
                ", lkmEmail='" + lkmEmail + '\'' +
                ", lkmPosition='" + lkmPosition + '\'' +
                ", lkmMemo='" + lkmMemo + '\'' +
                '}';
    }
}

Dao接口

public interface LinkManDao extends JpaRepository<LinkMan, Long>, JpaSpecificationExecutor<LinkMan> {
}

public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
}

测试类

package pers.zhang;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import pers.zhang.dao.CustomerDao;
import pers.zhang.dao.LinkManDao;
import pers.zhang.entity.Customer;
import pers.zhang.entity.LinkMan;

import java.util.Set;

/**
 * @author zhang
 * @date 2019/12/17 - 23:25
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class QueryByObjectTest {
    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private LinkManDao linkManDao;


    /*
        对象导航查询:查询一个对象的时候,通过此对象查询所有关联对象
            默认使用的是延迟加载的形式查询的
                调用get方法并不会立即发送查询,而是在使用关联对象的时候才会查询
            如果不像使用延迟加载:
                修改配置:fetch,需要配置到多表映射关系的注解上(OneToMany、ManyToMany、OneToOne)
     */
    @Test
    @Transactional//解决no session问题
    public void testQuery1(){
        Customer customer = customerDao.getOne(10L);
        //对象导航查询,此客户下的所有联系人
        Set<LinkMan> linkMans = customer.getLinkMans();

        for(LinkMan l : linkMans)
            System.out.println(l);

        /*
            Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
            Hibernate: select linkmans0_.lkm_cust_id as lkm_cust9_1_0_, linkmans0_.lkm_id as lkm_id1_1_0_, linkmans0_.lkm_id as lkm_id1_1_1_, linkmans0_.lkm_cust_id as lkm_cust9_1_1_, linkmans0_.lkm_email as lkm_emai2_1_1_, linkmans0_.lkm_gender as lkm_gend3_1_1_, linkmans0_.lkm_memo as lkm_memo4_1_1_, linkmans0_.lkm_mobile as lkm_mobi5_1_1_, linkmans0_.lkm_name as lkm_name6_1_1_, linkmans0_.lkm_phone as lkm_phon7_1_1_, linkmans0_.lkm_position as lkm_posi8_1_1_ from cst_linkman linkmans0_ where linkmans0_.lkm_cust_id=?
            LinkMan{lkmId=3, lkmName='二狗', lkmGender='null', lkmPhone='null', lkmMobile='null', lkmEmail='null', lkmPosition='null', lkmMemo='null'}
            LinkMan{lkmId=1, lkmName='小李', lkmGender='null', lkmPhone='null', lkmMobile='null', lkmEmail='null', lkmPosition='null', lkmMemo='null'}
         */

    }


    /*
        对象导航查询:查询一个对象的时候,通过此对象查询所有关联对象
     */
    @Test
    @Transactional//解决no session问题
    public void testQuery2(){
        Customer customer = customerDao.getOne(10L);
        //对象导航查询,此客户下的所有联系人
        Set<LinkMan> linkMans = customer.getLinkMans();

        for(LinkMan l : linkMans)
            System.out.println(l);

        /*
            Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=?
            Hibernate: select linkmans0_.lkm_cust_id as lkm_cust9_1_0_, linkmans0_.lkm_id as lkm_id1_1_0_, linkmans0_.lkm_id as lkm_id1_1_1_, linkmans0_.lkm_cust_id as lkm_cust9_1_1_, linkmans0_.lkm_email as lkm_emai2_1_1_, linkmans0_.lkm_gender as lkm_gend3_1_1_, linkmans0_.lkm_memo as lkm_memo4_1_1_, linkmans0_.lkm_mobile as lkm_mobi5_1_1_, linkmans0_.lkm_name as lkm_name6_1_1_, linkmans0_.lkm_phone as lkm_phon7_1_1_, linkmans0_.lkm_position as lkm_posi8_1_1_ from cst_linkman linkmans0_ where linkmans0_.lkm_cust_id=?
            LinkMan{lkmId=3, lkmName='二狗', lkmGender='null', lkmPhone='null', lkmMobile='null', lkmEmail='null', lkmPosition='null', lkmMemo='null'}
            LinkMan{lkmId=1, lkmName='小李', lkmGender='null', lkmPhone='null', lkmMobile='null', lkmEmail='null', lkmPosition='null', lkmMemo='null'}
         */

    }


    /*
        从多的一方查询:从联系人导航查询他所属客户
            默认为立即加载

        本质为:
            从一方查询多方
				* 默认:使用延迟加载(****)

			从多方查询一方
				* 默认:使用立即加载
     */
    @Test
    @Transactional//解决no session问题
    public void testQuery3(){
        LinkMan linkMan = linkManDao.getOne(2l);
        //对象导航查询所属客户
        Customer customer = linkMan.getCustomer();
        System.out.println(customer);

        /*
            Hibernate: select linkman0_.lkm_id as lkm_id1_1_0_, linkman0_.lkm_cust_id as lkm_cust9_1_0_, linkman0_.lkm_email as lkm_emai2_1_0_, linkman0_.lkm_gender as lkm_gend3_1_0_, linkman0_.lkm_memo as lkm_memo4_1_0_, linkman0_.lkm_mobile as lkm_mobi5_1_0_, linkman0_.lkm_name as lkm_name6_1_0_, linkman0_.lkm_phone as lkm_phon7_1_0_, linkman0_.lkm_position as lkm_posi8_1_0_, customer1_.cust_id as cust_id1_0_1_, customer1_.cust_address as cust_add2_0_1_, customer1_.cust_industry as cust_ind3_0_1_, customer1_.cust_level as cust_lev4_0_1_, customer1_.cust_name as cust_nam5_0_1_, customer1_.cust_phone as cust_pho6_0_1_, customer1_.cust_source as cust_sou7_0_1_ from cst_linkman linkman0_ left outer join cst_customer customer1_ on linkman0_.lkm_cust_id=customer1_.cust_id where linkman0_.lkm_id=?
            Customer{custId=11, custName='育碧', custSource='null', custIndustry='null', custLevel='null', custAddress='null', custPhone='null'}
         */
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值