java - 如何仅在@NamedEntityGraph的子图中加载指定的属性

我想从数据库中加载一个userreference对象,但从verifier属性中,我只想加载id、firstname和lastname,这样userreference看起来像这样:

{
  "id": 1,
  "company": "company1",
  "companyContactName": "some name",
  "companyPosition": "programmer",
  "referenceDate": "02/04/2005",
  "verifier": {
        "id":1
        "firstName": "Jane",
        "lastName": "Smith"
        "email":null,
        "username":null,
        "office:null,
        "department":null
  }
}

我为userreference类使用了一个实体图,但是我使用的这个实体图加载了用户拥有的所有信息,包括电子邮件、用户名、办公室和部门。
是否有任何方法可以指定类似EntityGraphType.Fetch的子图,以便它只加载验证器的ID、名字和姓氏?
这是我的用户参考:
public interface UserReferenceRepository extends JpaRepository<UserReference, Long>{

    @EntityGraph(value = "userReferenceGraph" ,  type = EntityGraphType.FETCH )
    UserReference findOne(Long id);
}

用户引用类:
@Getter
@Setter
@EqualsAndHashCode (exclude = {"id", "verifier"})
@ToString(exclude = {"id"})
@Entity
@NamedEntityGraphs({
    @NamedEntityGraph(
        name = "userReferenceGraph",      
        attributeNodes = {
            @NamedAttributeNode(value = "verifier", subgraph = "verifierGraph")
    },
    subgraphs = {
        @NamedSubgraph( 
            name = "verifierGraph",
            type = User.class,
            attributeNodes = {
                @NamedAttributeNode(value = "id"),
                @NamedAttributeNode(value = "firstName"),
                @NamedAttributeNode(value = "lastName")})})
})

public class UserReference {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", referencedColumnName = "user_id", foreignKey = @ForeignKey (name = "FK_UserReference_UserHRDetails_user_id"))
    @JsonIgnore
    private UserHRDetails hrDetails;

    private String company;
    private String companyContactName;
    private String companyPosition;
    private Date referenceDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "verifier_id")
    private User verifier;
}

和用户:
@Getter @Setter
@EqualsAndHashCode(exclude = {"id", "department", "company", "authorities", "hrDetails"})
@ToString(exclude = {"password"})
@Entity
@AllArgsConstructor
@Builder
public class User implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Access(value = AccessType.PROPERTY)
    private Long id;  

    @Size(max = 50)
    @Column(name = "first_name", length = 50)
    private String firstName;

    @Size(max = 50)
    @Column(name = "last_name", length = 50)
    private String lastName;

    @Column(length = 100, unique = true, nullable = false)
    private String email;

    @Column(length = 50, unique = true, nullable = false)
    private String username;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "department_id")
    private Department department;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "office_id")
    private Office office;
}

最佳答案

我遇到过同样的问题,我发现有两种解决方法:
快速:
您可以在您的实体中执行一些@postload操作,并取消不需要的字段。

@PostLoad
private void postLoad() {
    if (verifier != null) {
        verifier.email = null;
        verifier.office = null;
        verifier.department = null;
    }
}

对的:
另一种方法是通过将实体转换为DTO来保护它们。创建单独的pojo并将用户和用户引用转换为该dto pojo类。在那里你肯定会有更多的控制你的反应。

本文翻译自 https://stackoverflow.com/questions/38376869/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

标签 java hibernate lazy-loading spring-data-jpa entitygraph


相关文章:

html - 棘手的懒惰加载问题

java - 有没有帮助知道Hibernate是否已经加载了一个属性?

java - 为什么不允许将toString()作为默认方法添加到接口? [重复]

java - 在Windows上从Java到C ++的高效数据传输

database - Hibernate @OneToMany在更新父级时从列表中删除子级

hibernate - struts2 +休眠java.lang.reflect.InvocationTargetException

c# - 插入新实例时未执行实体框架核心延迟加载

java - 将JTextPane设置为内容类型HTML并使用字符串构建器

java - 获取JavaScript错误:使用断言来验证JavaScript警报文本时,“ e为null”

java - Spring + Hibernate + Envers +多线程 - 会话关闭