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