V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
reid2017
V2EX  ›  Java

Jpa / Hibernate 实体关联对象里的字段能否 exclude 部分字段

  •  
  •   reid2017 · Jun 27, 2019 · 2955 views
    This topic created in 2496 days ago, the information mentioned may be changed or developed.
    
    public class Student {
        private Integer id;
        private String name;
        private Integer age;
        private Integer gender;
        private LocalDate birthday;
    }
    
    public class Course {
        private Integer id;
        private String name;
        private String xxx;
        private String aaa;
    
        private Integer studentId;
        @JoinColumn(name = "studentId", insertable = false, updatable = false)
        @OneToOne(fetch = FetchType.EAGER)
        private Student student;
    }
    
    

    hibernate 在查询的时候,有没有办法可以 exclude 掉关联实体的部分字段?

    如上,在查 Course 时,关联的 Student 对象的全部字段会被查询出来,有没有办法可以过滤掉部分字段,只查询部分需要的字段?

    4 replies    2019-06-28 10:07:10 +08:00
    wc951
        1
    wc951  
       Jun 27, 2019 via Android
    延迟加载
    itechify
        2
    itechify  
    PRO
       Jun 27, 2019 via Android
    fetchtype 使用 lazy
    ilumer
        3
    ilumer  
       Jun 27, 2019
    entityGraph ,querydsl ,jpql 不过这些都是类似 DTO 的方式查询出来。延迟加载 整个实体只会被查询出 id,如果调用其他的属性会出现 N+1 的问题
    adzchao
        4
    adzchao  
       Jun 28, 2019
    lazy 是唯一解决方式
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3853 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 00:45 · PVG 08:45 · LAX 17:45 · JFK 20:45
    ♥ Do have faith in what you're doing.