JPA 2.1 Eager Fetch Attribute

时间:2014-04-09 08:00:46

标签: java hibernate jpa-2.1

我有一个属性(MetaData中的creationTimestamp),我注释它加载了lazy。不,我需要一个实体加载渴望。我尝试使用获取图表但没有成功。是否有一个JOIN FETCH类似的功能,我可以用它来查询它?

我正在使用Wildfly和捆绑的休眠。

当我尝试使用fetch图获取它时,我得到了hibernate无法找到id字段的异常。该字段位于实体库(受保护)中。

@NamedQueries({ @NamedQuery(name = ChatMessage.FIND_BY_CHAT_TIME, query = "SELECT m FROM ChatMessage m "
        + "WHERE m.chat=:chat "
        + "AND m.creationTimestamp > :index ORDER BY m.creationTimestamp") })
@NamedEntityGraph(
        name = ChatMessage.GRAPH_FETCH_ALL,
        attributeNodes = {@NamedAttributeNode("id"), @NamedAttributeNode("player"), @NamedAttributeNode("text"), @NamedAttributeNode("creationTimestamp")}
    )
public class ChatMessage extends EntityBase {

@MappedSuperclass
public abstract class EntityBase extends MetaData {
    private static final long serialVersionUID = -5579667581450362176L;

    public static final String FETCH_GRAPH_HINT = "javax.persistence.fetchgraph";

    @Id
    @Column(length = 36)
    protected String id = java.util.UUID.randomUUID().toString();

@MappedSuperclass
public class MetaData implements Serializable {
    private static final long serialVersionUID = 8870539357817188030L;

    @Version
    private long version;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false, updatable = false)
    @Basic(fetch=FetchType.LAZY)
    protected Calendar creationTimestamp;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false)
    @Basic(fetch=FetchType.LAZY)
    private Calendar updateTimestamp;

导致此异常:

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [id] on this ManagedType [ChatMessage]
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:144)
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getDeclaredAttribute(AbstractManagedType.java:137)
    at org.hibernate.jpa.graph.internal.EntityGraphImpl.resolveAttribute(EntityGraphImpl.java:139)
    at org.hibernate.jpa.graph.internal.AbstractGraphNode.buildAttributeNode(AbstractGraphNode.java:119)
    at org.hibernate.jpa.graph.internal.AbstractGraphNode.addAttribute(AbstractGraphNode.java:114)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.applyNamedAttributeNodes(EntityManagerFactoryImpl.java:279)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.applyNamedEntityGraphs(EntityManagerFactoryImpl.java:266)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:173)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:865)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:399)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:73)

有没有办法像抓取连接一样获取属性?或者我是否必须以其他方式引用获取图?

最好的问候, 米

1 个答案:

答案 0 :(得分:1)

看起来子类的属性必须以不同的方式引用:

@NamedEntityGraph(
        name = ChatMessage.GRAPH_FETCH_ALL,
        attributeNodes = {@NamedAttributeNode("player"), @NamedAttributeNode("text")}
        , subgraphs={@NamedSubgraph(name="MetaData.subgraph", attributeNodes={@NamedAttributeNode("id"), @NamedAttributeNode("creationTimestamp")})}
    )