Hi,
I have a 1:M rel between Company and Employee.
I need to read all Employees for a specifc Company.
My primary key is two integers:
code snippet:
Code:
@Embeddable
public class EntityOid implements Comparable, Serializable {
private Integer c;
private Integer i;
public EntityOid(Integer c, Integer i) {
this.c = c;
this.i = i;
}
}
It is referenced as pk in both entities:
Code snippet for Employee:
Code:
public class Employee {
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumns({
@JoinColumn(name="I_COMPANY_EMPLOYEES", referencedColumnName="I"),
@JoinColumn(name="C_COMPANY_EMPLOYEES", referencedColumnName="C")})
private Company company;
public Company getCompany(){
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}
Code snippet for Company:
Code:
public class Company{
@OneToMany(cascade=CascadeType.PERSIST, mappedBy="company")
@OrderBy("pk.c, pk.i ASC")
private List <Employee> employees = new ArrayList();
}
I try to execute thie query by doing:
Code:
Query query = em.createQuery("SELECT E FROM Employee E " +
"where E.company.pk.I = " + new Integer(55));
Note - I leave out the C just to keep it simple.
This query won't work, any ideas what I am doing wrong?
the exception I get is:
org.hibernate.QueryException: could not resolve property: company.pk.I of: Employee [SELECT E FROM Employee E where E.company.pk.I = 50275]
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1302)
at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:280)
at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:373)
at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:539)
at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:221)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:172)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:737)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1215)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4032)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3518)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1758)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:227)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:159)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:110)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1612)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:76)[/code]