-->
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.  [ 5 posts ] 
Author Message
 Post subject: New to hibernate - foreign key error
PostPosted: Mon Apr 16, 2007 11:20 am 
Newbie

Joined: Mon Apr 16, 2007 10:44 am
Posts: 9
I am new to Hibernate and am having some problems. I have the following structure:

========================================

Database table: content
Primary key: int content_id
Foreign keys: int siteId (sites.site_id), int categoryId (category.category_id)

Database table: categories
Primary key: int category_id

Database table: sites
Primary key: int site_id

========================================

I have created the Java POJO's as per my database structure. The current mapping files are throwing an error because the many-to-one relationship is of type object (CategoryObject/SiteObject) but my POJO defines the foreign key as an int.

What approach should I be taking???

If I change my POJO's set/getCategoryId and set/getSiteId to objects rather than int it will not throw an error - but then how do I set these int values when saving a record to the database?

Any help would be greatly appreciated.

========================================

Hibernate version: 3.2.3

Mapping documents:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="foo.object">
    <class name="ContentObject" table="CONTENT">
        <id name="id" column="content_id">
            <generator class="native"/>
        </id>
       
        <many-to-one name="siteId" column="site_id" class="SiteObject" />
        <many-to-one name="categoryId" column="category_id" class="CategoryObject" />
    </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="foo.object">
    <class name="CategoryObject" table="CATEGORIES">
        <id name="id" column="category_id">
            <generator class="native"/>
        </id>
       
        <property name="name" type="string" length="255" />
    </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="foo.object">
    <class name="SiteObject" table="SITES">
        <id name="id" column="site_id">
            <generator class="native"/>
        </id>
       
        <property name="name" type="string" length="255" />
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
Query q = sess.createQuery("from ContentObject");
List messages = q.list();
System.out.println(messages.size() + " message(s) found:" );


Full stack trace of any exception that occurs:

Class: org.hibernate.property.BasicPropertyAccessor$BasicSetter
Timestamp: 2007-04-17 00:54:35,437
Origin: org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPro
pertyAccessor.java:98)
Message: expected type: int, actual value: foo.object.SiteObject$$Enhan
cerByCGLIB$$8acb9221

Name and version of the database you are using: MySQL 4.1


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 11:57 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Please post your entities Java code.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 1:24 am 
Newbie

Joined: Thu Mar 29, 2007 7:03 am
Posts: 9
Location: Delhi
I also have the same problem.

_________________
Jini


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 9:16 am 
Newbie

Joined: Mon Apr 16, 2007 10:44 am
Posts: 9
I thought about it some more and found the solution to my problem.

Whereas before I declared my foreign keys as integers, they should in fact be declared as objects. Taking my previous code, the solution was changing my POJO from:

Code:
public class ContentObject {
   private int content_id;
   private int siteId; // Foreign key
   private int categoryId; // Foreign key

   ... (Get and Set methods)
}


to:

Code:
public class ContentObject {
   private int content_id;
   private SiteObject siteId; // Foreign key
   private CategoryObject categoryId; // Foreign key

   ... (Get and Set methods)
}


Which meant that for the way I saved a ContentObject to the database went from:

Code:
ContentObject newObj = new ContentObject();

// Save the int values
newObj.setSiteId(4);
newObj.setCategoryId(15);

sess.save(newObj); // sess is the Hibernate session


to:

Code:
ContentObject newObj = new ContentObject();
// Load the foreign key objects from the database
SiteObject siteObj = (SiteObject)sess.load(SiteObject.class, 4);
CategoryObject categoryObj = (CategoryObject)sess.load(CategoryObject.class, 15);

// Save the objects rather than the int values
newObj.setSiteId(siteObj);
newObj.setCategoryId(categoryObj);

sess.save(newObj); // sess is the Hibernate session


This will insert a new row in my content table with the integer values of 4 for site_id and 15 for category_id. No entries are made to the site or category tables unless I specify an int that doesn't exist in the foreign table.

When you think about it, handling foreign keys this way makes sense. Example: If I wanted to set siteId(999) which doesn't exist, it would first be saved to the site table. I can't just set the id to a number that doesn't exist in the site table.

I hope this helps. Please let me know if this method is not the best way, but for the moment it works.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 9:44 am 
Newbie

Joined: Thu Mar 29, 2007 7:03 am
Posts: 9
Location: Delhi
Hi
In your case If I m executing select query on child table it also fetches the record from the referenced parent table while we are not required data from parent table. This will make performance impact.

Also at the time of insertion/updation I have to create unneccesary object of parent type whose details I dont know.

Time being I m also doing the same but this does not looks the proper solution.

_________________
Jini


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.