Hibernate忽略了@NamedEntityGraph

时间:2017-11-07 12:22:22

标签: java spring hibernate

我们希望在会员(我们的虚构俱乐部)与他所付款之间建立关系。 因此,我们创建了一个实体"成员" 和一个实体" premiums"

成员现在应该是他过去所做的所有付款。如果他没有付款,则该列表应为空。我们希望加载 成员 lazy 以减少数据库操作量。

成员如下所示:

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Entity
@NamedEntityGraph(name = "graph.Member.payments",
                 attributeNodes = @NamedAttributeNode("payments"))
public class Member implements Serializable {
     private static final long serialVersionUID = 6925248180274039273L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long memberID;

@OneToMany(mappedBy = "member", fetch = FetchType.LAZY)
private List<Premium> payments;

premium 这样的实体:

import javax.persistence.*;
import java.io.Serializable;
import javax.persistence.JoinColumn;

@Entity
public class Premium implements Serializable{

private String year;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long paymentID;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="memberID")
private Member member;

我们有一个查询,我们希望获得具有特定ID的成员。查询如下所示:

     public List<Member> findAll(long id) {
     EntityGraph  postGraph=entityManager.getEntityGraph("graph.Member.payments");
        return entityManager
                .createQuery("select m from Member m where m.memberID = (:id)")
                .setHint("javax.persistence.loadgraph", postGraph)
                .setParameter("id", id)
                .getResultList();
    }

如果我们执行该方法, hibernate并不似乎加载我们设置的提示。 在我们的服务器日志中,我们可以看到创建 以下查询:

Hibernate: 
select
    member0_.memberID as memberID1_0_0_,
    member0_.accountRelationship as accountR2_0_0_,
    member0_.address as address3_0_0_,
    member0_.birthday as birthday4_0_0_,
    member0_.dateOfSeperation as dateOfSe5_0_0_,
    member0_.entryDate as entryDat6_0_0_,
    member0_.family as family7_0_0_,
    member0_.firstName as firstNam8_0_0_,
    member0_.memberShip as memberSh9_0_0_,
    member0_.name as name10_0_0_,
    member0_.terminationDate as termina11_0_0_ 
from
    Member member0_ 
where
    member0_.memberID=?
07-Nov-2017 13:11:47.227 WARNING [http-nio-8080-exec-6] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: de.nordakademie.iaa.roommgmt.model.Member.payments, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: de.nordakademie.iaa.roommgmt.model.Member.payments, could not initialize proxy - no Session (through reference chain: de.nordakademie.iaa.roommgmt.model.Member["payments"])

对于我们的项目,我们使用 tomcat 8.5.5,hibernate 5.2.10,spring 4.3.11,H2 1.4.196和AngularJS 1.6

如果有任何不清楚的地方,请随时问我们。

1 个答案:

答案 0 :(得分:0)

您可以查看

.setHint("javax.persistence.fetchgraph", postGraph)

而不是'loadgraph'。 Hibernate的行为有时是不可预测的。