命名的Hibernate查询的“命名的查询未知”?

rhfm7lfc  于 2023-10-23  发布在  其他
关注(0)|答案(2)|浏览(134)

有什么问题吗?

@Entity
@NamedQueries( {
    @NamedQuery(name = User.ALL,
                query = "SELECT u FROM User u")
})

public class User {
    public static final String ALL = "User.all";
}

public class Service {
    find... with ... User.ALL
}

堆栈跟踪:

Caused by: org.hibernate.MappingException: Named query not known: User.all
    at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)
    at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1407)
    at $Session_a5ad46cfa25.getNamedQuery(Unknown Source)
    at $Session_a5ad46cf913.getNamedQuery(Unknown Source)
    at de.medicshare.dal.HibernateCrudServiceDAO.findUniqueWithNamedQuery(HibernateCrudServiceDAO.java:83)
    at $CrudServiceDAO_a5ad46cfa19.findUniqueWithNamedQuery(Unknown Source)
    at $CrudServiceDAO_a5ad46cfa1b.findUniqueWithNamedQuery(Unknown Source)
    at $CrudServiceDAO_a5ad46cf931.findUniqueWithNamedQuery(Unknown Source)
    at de.medicshare.pages.Signup.proceedSignup(Signup.java:82)
    at de.medicshare.pages.Signup.dispatchComponentEvent(Signup.java)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.dispatchEvent(ComponentPageElementImpl.java:923)
    at org.apache.tapestry5.internal.structure.ComponentPageElementImpl.processEventTriggering(ComponentPageElementImpl.java:1106)
    ... 90 more
lnxxn5zx

lnxxn5zx1#

不知道这是否是你的问题,但看看这篇文章here
问题是使用:

org.hibernate.annotations.Entity

而不是:

javax.persistence.Entity
pod7payv

pod7payv2#

当我在包中重命名一个Entity类时,我也遇到了类似的问题。确保persistence.xml中持久性单元的完整类名与Entity类匹配。例如

<persistence-unit name="your_PU_name" transaction-type="JTA">
      ...
      <class> [ make sure here is the FQN of your entity class ] </class>
      ...
   </persistence-unit>

相关问题