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

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

我想从数据库加载一个 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、firstName 和 lastName?

这是我的 UserReferenceRepository:

public interface UserReferenceRepository extends JpaRepository<UserReference, Long>{

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

UserReference 类:

@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;
}

最佳答案

我想您正在使用 Jackson 生成 JSON。在这种情况下,这是一场 Jackson 与 Entity Graph 的战斗,前者没有机会赢得这场战斗。 Entity Graph 只是构建 SQL 查询的提示,您只能告诉 Hibernate 不要加载某些属性。 Hibernate 在加载基本实体字段时仍然不支持实体图,请参阅 https://hibernate.atlassian.net/browse/HHH-9270 .但主要问题是 Jackson 会在 JSON 生成期间调用实体中的每个 getter,而 Hibernate 会延迟加载它们而不考虑实体图。我只能建议使用 @JsonIgnore,但这可能没有您需要的那么灵活。

关于java - 如何在@NamedEntityGraph 中仅加载子图中的指定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38376869/

相关文章:

java - 如何处理Socket accept failed Error is coming In in Jsp Project

java - 如何使用 lambda 从 List<> 中获取总和和最低日期

java - 更新 TextView 导致 Android 应用程序崩溃

postgresql - 重启 Spring Boot 应用程序后首次调用速度慢

jquery - 延迟 <option> 加载

C# Entity Framework 延迟加载(如果未分离)

java - removeAll() 时 TreeSet 中的 NullPointerException

java - 在事件调用期间与 hibernate session 交互

mysql - Spring MVC 中的数据库问题 - HIbernate 示例

c# - 如何在不等待的情况下在 Singleton 中执行昂贵的操作?