Help needed for hibernate foreign key mapping.

Discussion in 'Java' started by shaji, Aug 29, 2005.

  1. shaji

    shaji Guest

    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.
     
    shaji, Aug 29, 2005
    #1
    1. Advertising

  2. shaji

    jan V Guest

    > 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.
     
    jan V, Aug 29, 2005
    #2
    1. Advertising

  3. shaji

    isitmeorthey Guest

    What are your fields mapped for in your User and Company classes ?
     
    isitmeorthey, Aug 29, 2005
    #3
  4. shaji

    shaji Guest

    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();
     
    shaji, Aug 30, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. venu

    Hibernate mapping help

    venu, Jan 13, 2006, in forum: Java
    Replies:
    0
    Views:
    417
  2. anita
    Replies:
    0
    Views:
    340
    anita
    Mar 14, 2006
  3. H5N1
    Replies:
    0
    Views:
    443
  4. snehasish
    Replies:
    0
    Views:
    1,630
    snehasish
    Oct 27, 2009
  5. Lionel
    Replies:
    9
    Views:
    3,408
Loading...

Share This Page