Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

java - Hibernate HQL with interfaces

According to this section of the Hibernate documentation I should be able to query any java class in HQL

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-polymorphism

Unfortunately when I run this query...

"from Transaction trans where trans.envelopeId=:envelopeId"

I get the message "Transaction is not mapped [from Transaction trans where trans.envelopeId=:envelopeId]".

Transaction is an interface, I have to entity classes that implement it, I want on HQL query to return a Collection of type Transaction.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Indeed, according to the Hibernate documentation on Polymorphic queries:

Hibernate queries can name any Java class or interface in the from clause. The query will return instances of all persistent classes that extend that class or implement the interface. The following query would return all persistent objects:

from java.lang.Object o

The interface Named might be implemented by various persistent classes:

from Named n, Named m where n.name = m.name

But because the interface is not mapped (and thus unknown), you need to use the fully qualified name in your HQL query:

from qualified.name.Transaction trans where trans.envelopeId=:envelopeId

This will return instances of all persistent classes that implement your Transaction interface.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...