-->
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: Foreign key return entity, how to get id?
PostPosted: Wed Feb 22, 2012 5:08 am 
Newbie

Joined: Wed Feb 22, 2012 4:29 am
Posts: 2
Hi, im new to hibernate. So after 2 days research for this error, i really give up and post here hoping for your helps. Here is the hbm class

Code:
from TblProject pro, TblEmployee emp where pro.managerID = emp.id

Code:
<hibernate-mapping>
    <class name="model.TblProject" table="tblProject" schema="dbo" catalog="PTS">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <many-to-one name="tblDepartment" class="model.TblDepartment" fetch="select">
            <column name="primaryDepartmentID" />
        </many-to-one>
        <many-to-one name="tblEmployee" class="model.TblEmployee" fetch="select">
            <column name="managerID" />
        </many-to-one>
        <property name="name" type="string">
            <column name="name" length="25" not-null="true" />
        </property>
        <property name="numberOfEmployee" type="int">
            <column name="numberOfEmployee" not-null="true" />
        </property>
        <property name="rank" type="int">
            <column name="rank" not-null="true" />
        </property>
        <property name="scope" type="string">
            <column name="scope" not-null="true" />
        </property>
        <set name="tblCheckpoints" inverse="true">
            <key>
                <column name="projectID" not-null="true" />
            </key>
            <one-to-many class="model.TblCheckpoint" />
        </set>
        <set name="tblProjectEmployees" inverse="true">
            <key>
                <column name="projectID" not-null="true" />
            </key>
            <one-to-many class="model.TblProjectEmployee" />
        </set>
    </class>
</hibernate-mapping>


Code:
<hibernate-mapping>
    <class name="model.TblEmployee" table="tblEmployee" schema="dbo" catalog="PTS">
        <id name="id" type="int">
            <column name="id" />
            <generator class="native" />
        </id>
        <many-to-one name="tblDepartment" class="model.TblDepartment" fetch="select">
            <column name="departmentID" not-null="true" />
        </many-to-one>
        <property name="firstName" type="string" >
            <column name="firstName" not-null="true" />
        </property>
        <property name="lastName" type="string">
            <column name="lastName" not-null="true" />
        </property>
        <property name="phone" type="string">
            <column name="phone" length="20" not-null="true" />
        </property>
        <property name="email" type="string">
            <column name="email" length="50" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="password" not-null="true" />
        </property>
        <property name="isAdmin" type="boolean">
            <column name="isAdmin" not-null="true" />
        </property>
        <set name="tblDepartments" inverse="true">
            <key>
                <column name="managerID" not-null="true" />
            </key>
            <one-to-many class="model.TblDepartment" />
        </set>
        <set name="tblCheckpointResults" inverse="true">
            <key>
                <column name="employeeID" not-null="true" />
            </key>
            <one-to-many class="model.TblCheckpointResult" />
        </set>
        <set name="tblCheckpointDetailResults" inverse="true">
            <key>
                <column name="employeeID" not-null="true" />
            </key>
            <one-to-many class="model.TblCheckpointDetailResult" />
        </set>
        <set name="tblProjectEmployees" inverse="true">
            <key>
                <column name="employeeID" not-null="true" />
            </key>
            <one-to-many class="model.TblProjectEmployee" />
        </set>
        <set name="tblProjects" inverse="true">
            <key>
                <column name="managerID" />
            </key>
            <one-to-many class="model.TblProject" />
        </set>
    </class>
</hibernate-mapping>


This is the error which i get
Code:
org.hibernate.QueryException: could not resolve property: managerID of: model.TblProject [from model.TblProject pro, model.TblEmployee emp where pro.managerID = emp.id]
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
   at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1358)
   at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:279)
   at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:386)
   at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:566)
   at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:241)
   at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:188)
   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:728)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4041)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3525)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762)
   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:228)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
   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:72)
   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:1623)

I can't do it as normal since in hibernate the managerID will be displayed as entity model.TblEmployee.
How can i fix this problem >"<? Please help me. Thanks


Top
 Profile  
 
 Post subject: Re: Foreign key return entity, how to get id?
PostPosted: Wed Feb 22, 2012 8:43 am 
Newbie

Joined: Wed Feb 22, 2012 4:29 am
Posts: 2
Add POJO class
Code:
package model;
// Generated Feb 15, 2012 11:28:05 PM by Hibernate Tools 3.2.1.GA


import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
* TblEmployee generated by hbm2java
*/
@Entity
@Table(name="tblEmployee"
    ,schema="dbo"
    ,catalog="PTS"
)
public class TblEmployee  implements java.io.Serializable {


     private int id;
     private TblDepartment tblDepartment;
     private String firstName;
     private String lastName;
     private String phone;
     private String email;
     private String password;
     private boolean isAdmin;
     private Set<TblDepartment> tblDepartments = new HashSet<TblDepartment>(0);
     private Set<TblCheckpointResult> tblCheckpointResults = new HashSet<TblCheckpointResult>(0);
     private Set<TblCheckpointDetailResult> tblCheckpointDetailResults = new HashSet<TblCheckpointDetailResult>(0);
     private Set<TblProjectEmployee> tblProjectEmployees = new HashSet<TblProjectEmployee>(0);
     private Set<TblProject> tblProjects = new HashSet<TblProject>(0);

    public TblEmployee() {
    }

   
    public TblEmployee(int id, TblDepartment tblDepartment, String firstName, String lastName, String phone, String email, String password, boolean isAdmin) {
        this.id = id;
        this.tblDepartment = tblDepartment;
        this.firstName = firstName;
        this.lastName = lastName;
        this.phone = phone;
        this.email = email;
        this.password = password;
        this.isAdmin = isAdmin;
    }
    public TblEmployee(int id, TblDepartment tblDepartment, String firstName, String lastName, String phone, String email, String password, boolean isAdmin, Set<TblDepartment> tblDepartments, Set<TblCheckpointResult> tblCheckpointResults, Set<TblCheckpointDetailResult> tblCheckpointDetailResults, Set<TblProjectEmployee> tblProjectEmployees, Set<TblProject> tblProjects) {
       this.id = id;
       this.tblDepartment = tblDepartment;
       this.firstName = firstName;
       this.lastName = lastName;
       this.phone = phone;
       this.email = email;
       this.password = password;
       this.isAdmin = isAdmin;
       this.tblDepartments = tblDepartments;
       this.tblCheckpointResults = tblCheckpointResults;
       this.tblCheckpointDetailResults = tblCheckpointDetailResults;
       this.tblProjectEmployees = tblProjectEmployees;
       this.tblProjects = tblProjects;
    }
   
     @Id
   
    @Column(name="id", unique=true, nullable=false)
    public int getId() {
        return this.id;
    }
   
    public void setId(int id) {
        this.id = id;
    }
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="departmentID", nullable=false)
    public TblDepartment getTblDepartment() {
        return this.tblDepartment;
    }
   
    public void setTblDepartment(TblDepartment tblDepartment) {
        this.tblDepartment = tblDepartment;
    }
   
    @Column(name="firstName", nullable=false)
    public String getFirstName() {
        return this.firstName;
    }
   
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
   
    @Column(name="lastName", nullable=false)
    public String getLastName() {
        return this.lastName;
    }
   
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
   
    @Column(name="phone", nullable=false, length=20)
    public String getPhone() {
        return this.phone;
    }
   
    public void setPhone(String phone) {
        this.phone = phone;
    }
   
    @Column(name="email", nullable=false, length=50)
    public String getEmail() {
        return this.email;
    }
   
    public void setEmail(String email) {
        this.email = email;
    }
   
    @Column(name="password", nullable=false)
    public String getPassword() {
        return this.password;
    }
   
    public void setPassword(String password) {
        this.password = password;
    }
   
    @Column(name="isAdmin", nullable=false)
    public boolean isIsAdmin() {
        return this.isAdmin;
    }
   
    public void setIsAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="tblEmployee")
    public Set<TblDepartment> getTblDepartments() {
        return this.tblDepartments;
    }
   
    public void setTblDepartments(Set<TblDepartment> tblDepartments) {
        this.tblDepartments = tblDepartments;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="tblEmployee")
    public Set<TblCheckpointResult> getTblCheckpointResults() {
        return this.tblCheckpointResults;
    }
   
    public void setTblCheckpointResults(Set<TblCheckpointResult> tblCheckpointResults) {
        this.tblCheckpointResults = tblCheckpointResults;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="tblEmployee")
    public Set<TblCheckpointDetailResult> getTblCheckpointDetailResults() {
        return this.tblCheckpointDetailResults;
    }
   
    public void setTblCheckpointDetailResults(Set<TblCheckpointDetailResult> tblCheckpointDetailResults) {
        this.tblCheckpointDetailResults = tblCheckpointDetailResults;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="tblEmployee")
    public Set<TblProjectEmployee> getTblProjectEmployees() {
        return this.tblProjectEmployees;
    }
   
    public void setTblProjectEmployees(Set<TblProjectEmployee> tblProjectEmployees) {
        this.tblProjectEmployees = tblProjectEmployees;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="tblEmployee")
    public Set<TblProject> getTblProjects() {
        return this.tblProjects;
    }
   
    public void setTblProjects(Set<TblProject> tblProjects) {
        this.tblProjects = tblProjects;
    }
}


Code:
package model;
// Generated Feb 15, 2012 11:28:05 PM by Hibernate Tools 3.2.1.GA


import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.ForeignKey;

/**
* TblProject generated by hbm2java
*/
@Entity
@Table(name="tblProject"
    ,schema="dbo"
    ,catalog="PTS"
)
public class TblProject  implements java.io.Serializable {


     private int id;
     private TblDepartment tblDepartment;
     private TblEmployee tblEmployee;
     private String name;
     private int numberOfEmployee;
     private int rank;
     private String scope;
     private Set<TblCheckpoint> tblCheckpoints = new HashSet<TblCheckpoint>(0);
     private Set<TblProjectEmployee> tblProjectEmployees = new HashSet<TblProjectEmployee>(0);

    public TblProject() {
    }

   
    public TblProject(int id, String name, int numberOfEmployee, int rank, String scope) {
        this.id = id;
        this.name = name;
        this.numberOfEmployee = numberOfEmployee;
        this.rank = rank;
        this.scope = scope;
    }
    public TblProject(int id, TblDepartment tblDepartment, TblEmployee tblEmployee, String name, int numberOfEmployee, int rank, String scope, Set<TblCheckpoint> tblCheckpoints, Set<TblProjectEmployee> tblProjectEmployees) {
       this.id = id;
       this.tblDepartment = tblDepartment;
       this.tblEmployee = tblEmployee;
       this.name = name;
       this.numberOfEmployee = numberOfEmployee;
       this.rank = rank;
       this.scope = scope;
       this.tblCheckpoints = tblCheckpoints;
       this.tblProjectEmployees = tblProjectEmployees;
    }
   
     @Id
   
    @Column(name="id", unique=true, nullable=false)
    public int getId() {
        return this.id;
    }
   
    public void setId(int id) {
        this.id = id;
    }
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="primaryDepartmentID")
    public TblDepartment getTblDepartment() {
        return this.tblDepartment;
    }
   
    public void setTblDepartment(TblDepartment tblDepartment) {
        this.tblDepartment = tblDepartment;
    }
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="managerID")
    @ForeignKey(name="FK_tblProject_tblEmployee")
    public TblEmployee getTblEmployee() {
        return this.tblEmployee;
    }
   
    public void setTblEmployee(TblEmployee tblEmployee) {
        this.tblEmployee = tblEmployee;
    }
   
    @Column(name="name", nullable=false, length=25)
    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
   
    @Column(name="numberOfEmployee", nullable=false)
    public int getNumberOfEmployee() {
        return this.numberOfEmployee;
    }
   
    public void setNumberOfEmployee(int numberOfEmployee) {
        this.numberOfEmployee = numberOfEmployee;
    }
   
    @Column(name="rank", nullable=false)
    public int getRank() {
        return this.rank;
    }
   
    public void setRank(int rank) {
        this.rank = rank;
    }
   
    @Column(name="scope", nullable=false)
    public String getScope() {
        return this.scope;
    }
   
    public void setScope(String scope) {
        this.scope = scope;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="tblProject")
    public Set<TblCheckpoint> getTblCheckpoints() {
        return this.tblCheckpoints;
    }
   
    public void setTblCheckpoints(Set<TblCheckpoint> tblCheckpoints) {
        this.tblCheckpoints = tblCheckpoints;
    }
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="tblProject")
    public Set<TblProjectEmployee> getTblProjectEmployees() {
        return this.tblProjectEmployees;
    }
   
    public void setTblProjectEmployees(Set<TblProjectEmployee> tblProjectEmployees) {
        this.tblProjectEmployees = tblProjectEmployees;
    }
}


Please help T_T. I'm out of mind :(


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.