Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help needed for hibernate foreign key mapping.

Reply
Thread Tools

Help needed for hibernate foreign key mapping.

 
 
shaji
Guest
Posts: n/a
 
      08-29-2005
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(Lo ader.java:650)
at
org.hibernate.loader.Loader.getRowFromResultSet(Lo ader.java:277)
at org.hibernate.loader.Loader.doQuery(Loader.java:38 4)
at
org.hibernate.loader.Loader.doQueryAndInitializeNo nLazyCollections(Loader.java:203)
at org.hibernate.loader.Loader.doList(Loader.java:149 9)
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.ja va:1246)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl. java:299)
at de.gloegl.road2hibernate.Start.main(Start.java:92)


Thanks,
shaji.

 
Reply With Quote
 
 
 
 
jan V
Guest
Posts: n/a
 
      08-29-2005
> 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(Lo ader.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.


 
Reply With Quote
 
 
 
 
isitmeorthey
Guest
Posts: n/a
 
      08-29-2005
What are your fields mapped for in your User and Company classes ?

 
Reply With Quote
 
shaji
Guest
Posts: n/a
 
      08-30-2005

isitmeorthey wrote:
> 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();

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hibernate foreign key as primary key Lionel Java 9 08-10-2011 04:58 PM
hibernate mapping for more than one foreign key to a single table snehasish Java 0 10-27-2009 09:53 AM
Survey - implementing CRUD with "foreign name" instead of foreign key. H5N1 ASP .Net 0 05-03-2006 11:36 PM
Hibernate Syncronizer now generates code for Hibernate 3.0 msenin@covad.net Java 0 07-14-2005 05:47 AM
[HIBERNATE] [EVALUATION] - Gavin King Censors Hibernate Forum Ilias Lazaridis Java 0 12-27-2004 04:26 AM



Advertisments