Question

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.

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top