How do I create Named Queries in a Separate file

£可爱£侵袭症+ 提交于 2019-12-12 16:16:40

问题


I need to keep the all the named queries in a separate file. For example

@javax.persistence.NamedQueries({
    @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")})

public class AllNamedQueries {

}

Now that much in itself causes an Error "Named queries can only be defined in Entity or MappedSuperClass class".

How do I create Named Queries in a Separate file? By separate I mean files other than entity file.


回答1:


You can use orm.xml that allows you to define JPA mappings in XML. This approach is equivalent and overrides annotations.

Example based on define named query in orm.xml with jpa and hibernate:

<entity class="com.example.Employee">
    <table name="Employee.findAll" />
    <named-query name="findAll">
        <query><![CDATA[
          SELECT e
          FROM Employee e
        ]]></query>
    </named-query>
</entity>


来源:https://stackoverflow.com/questions/8407799/how-do-i-create-named-queries-in-a-separate-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!