6

I have an entity named Client, And it has some named queries and native named queries. What I want to do is, I want to move this named queries to another class. For that I would like to extend the Client entity by another class ClientQuery. And move all named,native queries to that class. Is it possible to do so?

Client CLASS

@XmlRootElement(name = "CLIENT_DETAILS")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@NamedQueries({
    @NamedQuery(name = Client.GET_CLIENT_BYLANGID,
    query = "select T from Client T where T.clientPK.langId=:langId")
})
public class Client implements Serializable {
public static final String GET_CLIENT_BYLANGID = "Client.getClientByLangId";
arjuncc
  • 3,227
  • 5
  • 42
  • 77

2 Answers2

1

As I understand, you want to know whether it correct to move the @NamedQuery out of the Entityclass to a non-entity class.

I have quickly checked the specification and did not see any restrictions about that. Additionally I have tried to put in an mapping.xml an <named-query> element outside of the <entity> element and it is xml-valid, so it is legal.

V G
  • 18,822
  • 6
  • 51
  • 89
0

If you move the @NamedQuery in a class that is not an entity that won´t work as hibernate only scan the classes that are entities. You will find something like NamedQuery not found, also it is a good practice to have them in the entity that has more reference to them.

Koitoer
  • 18,778
  • 7
  • 63
  • 86