Help needed for hibernate foreign key mapping.

S

shaji

Hi:
I have a Company table and Company class. The mapping file for Company
looks like this.
<hibernate-mapping>
<class name="de.gloegl.road2hibernate.Company" table="si_company">
<id name="company_id" column="si_company_id" >
<generator class="increment"/>
</id>
<property name="company_name" column="si_company_name"/>
</class>
</hibernate-mapping>


Also I have a User table and User class. The mapping file for User
looks like this. Here company_id is a foreign key from table
si_company.

<hibernate-mapping>
<class name="de.gloegl.road2hibernate.User" table="si_user">
<composite-id>
<key-many-to-one name="user_id" column="si_user_id"/>
<key-many-to-one name="company_id" column="si_company_id"/>
</composite-id>
<property name="user_email" column="si_user_name"/>
</class>
</hibernate-mapping>



When I try to load/find a User from the si_user I am getting an error
like this. Can anyone please point out, where I am getting wrong.

Exception in thread "main" java.lang.ClassCastException
at
org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:650)
at
org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:277)
at org.hibernate.loader.Loader.doQuery(Loader.java:384)
at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:203)
at org.hibernate.loader.Loader.doList(Loader.java:1499)
at org.hibernate.loader.Loader.list(Loader.java:1482)
at
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1246)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)
at de.gloegl.road2hibernate.Start.main(Start.java:92)


Thanks,
shaji.
 
J

jan V

When I try to load/find a User from the si_user I am getting an error
like this. Can anyone please point out, where I am getting wrong.

Exception in thread "main" java.lang.ClassCastException
at
org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:650)

This is a hibernate exception which, IMO, it should not throw in this
manner. OK, you're doing something wrong which lets Hibernate slam into a
ClassCastException... but I think this exception is highly
programmer-unfriendly. Hibernate should catch this exception and rethrow a
Hibernate-specific, and programmer-friendly exception... one that explains
in a lot more detail why Hibernate couldn't proceed. ClassCastException is
such a moronic exception for not telling us at the very least what type it
encountered, and what type it wanted to cast to.
 
S

shaji

isitmeorthey said:
What are your fields mapped for in your User and Company classes ?

The fields mapped for User class is user_id, company_id, user_email
The fields mapped for Company class is company_id, company_name

I am getting this exception for the following line.
List result = hs.createCriteria(User.class)
.add(Restrictions.like("company_id", new Long(1)))
.add(Restrictions.like("user_id", new Long(1)))
.list();


The following line also throws the same exception.

Query q = hs.createQuery("from User as user where user.user_id =
:userID
and user.company_id= :companyID");
q.setParameter("userID",new Integer(1));
q.setParameter("companyID",new Integer(1));
List result = q.list();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top