Hey there,
I have a problem when I try to query entities that have a specific embeddable in their element collection.
Given the following entity:
public class Team extends AbstractEntity {
....
@CollectionTable(name = "team_member",
joinColumns = @JoinColumn(name = "team"))
@AttributeOverride(name = "resourceBasePath",
column = @Column(name = "user_base_path")),
@AttributeOverride(name = "id",
column = @Column(name = "user_id")),
@AttributeOverride(name = "tenant",
column = @Column(name = "user_tenant"))
private Set<EntityReference> members = newHashSet();
....
}
As you can see, I have a Team class that has members. The members field is an element collection of the embeddable EntityReference. Please also note the AttributeOverrides that overrides the column names of the EntityReference in this particular case.
My problem right now is that I'm not able to query teams that have a specific member.
When I execute this query:
@Override
public List<Team> findByMember(final EntityReference userReference) {
isNotNull(userReference,
"userReference");
return selectFromEntity().where(QTeam.team.members.any().eq(userReference))
.list(QTeam.team);
}
then I get the exception: javax.persistence.PersistenceException: org.hibernate.HibernateException: SqlNode's text did not reference expected number of columns.
When I execute this query:
@Override
public List<Team> findByMember(final EntityReference userReference) {
isNotNull(userReference,
"userReference");
return selectFromEntity().where(QTeam.team.members.contains(userReference))
.list(QTeam.team);
then I get the exception: javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not determine a type for class: com.arviem.common.model.EntityReference.
When I execute the query:
@Override
public List<Team> findByMember(final EntityReference userReference) {
isNotNull(userReference,
"userReference");
final QEntityReference member = new QEntityReference("members");
return selectFromEntity().leftJoin(QTeam.team.members,
member)
.where(member.eq(userReference))
.list(QTeam.team);
}
then I get the exception: javax.persistence.PersistenceException: org.hibernate.HibernateException: SqlNode's text did not reference expected number of columns.
When I execute the query:
@Override
public List<Team> findByMember(final EntityReference userReference) {
isNotNull(userReference,
"userReference");
final QEntityReference member = new QEntityReference("members");
return selectFromEntity().leftJoin(QTeam.team.members,
member)
.where(member.resourceBasePath.eq(userReference.getResourceBasePath()),
member.tenant.eq(userReference.getTenant()),
member.id.eq(userReference.getId()))
.list(QTeam.team);
}
then I get an exception like: ... Caused by: org.h2.jdbc.JdbcSQLException: Column "MEMBERS1_.ID" not found ...
which means that the AttributeOverrides were not considered in this case, because the column "MEMBERS1_.ID" does actually not exist; it should have been "MEMBERS1_.USER_ID".
I saw that others managed to get Q classes generated for the element collection of embeddables. In the post above, the user is talking about a Q class called QB_C where B would be the parent entity and C would stand for the embeddables. I assume that these Q classes would help me to do the query right but unfortunately I do not get these Q classes generated and I couldn't find out what is required in order to get them generated.
Any help would be appreciated as I have to do a native query right now to solve this (eeuw).