-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Using a Query to read a relationship with a foreign key
PostPosted: Mon Jun 25, 2007 1:36 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
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]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 26, 2007 11:37 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
I found out what I was doing wrong.

The query should have been:

<code>
Query query = em.createQuery("SELECT E FROM Employee E " +
"where E.company.pk.i = " + 55);
</code>
i.e. lowercase i not uppercase. Hibernate is case sensitive for attributes.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.