Querying collections of value type in the Criteria API in Hibernate
Question
In my database, I have an entity table (let's call it Entity). Each entity can have a number of entity types, and the set of entity types is static. Therefore, there is a connecting table that contains rows of the entity id and the name of the entity type. In my code, EntityType is an enum, and Entity is a Hibernate-mapped class.
in the Entity code, the mapping looks like this:
@CollectionOfElements
@JoinTable(
name = "ENTITY-ENTITY-TYPE",
joinColumns = @JoinColumn(name = "ENTITY-ID")
)
@Column(name="ENTITY-TYPE")
public Set<EntityType> getEntityTypes() {
return entityTypes;
}
Oh, did I mention I'm using annotations?
Now, what I'd like to do is create an HQL query or search using a Criteria for all Entity objects of a specific entity type.
This page in the Hibernate forum says this is impossible, but then this page is 18 months old. Can anyone tell me if this feature has been implemented in one of the latest releases of Hibernate, or planned for the coming release?
Solution
HQL:
select entity from Entity entity where :type = some elements(entity.types)
I think that you can also write it like:
select entity from Entity entity where :type in(entity.types)
OTHER TIPS
Is your relationship bidirectional, i.e., does EntityType
have an Entity
property? If so, you can probably do something like entity.Name from EntityType where name = ?