define named query in orm.xml with jpa and hibernate

后端 未结 4 2010
既然无缘
既然无缘 2020-12-05 10:55

I\'m trying to put my named queries in my orm.xml (put in META-INF with persistence.xml) but my orm.xml seems to be ignored by hibernate/jpa.

When I try to create m

4条回答
  •  执笔经年
    2020-12-05 11:30

    As said

    When I try to create my named query with em.createNamedQuery("myQuery"), it returns that it can not find this query.

    You are right. But you forget the following

    If you place a named query definition inside a element, instead of the root, it is prefixed with the name of the entity class

    So you need to call your namedQuery as

     em.createNamedQuery("Account.myQuery")
    

    I am curious: Does your Account class is stored in the root classpath ??? If not, you have fix its missing package. Suppose Account class is stored inside br.com.hibernate.model.domain.Account. So you should declare your entity as

    And you need to call your namedQuery as

    em.createNamedQuery("br.com.hibernate.model.domain.Account.myQuery") instead
    

    Just an adivice: when you are using Hibernate as your Persistence Provider, you do not need to define your Entity class in persistence.xml file.

    regards,

提交回复
热议问题