The problem:
As soon as i add the many-to-one associations to my hibernate mapped User class below i get a Duplicate key or integrity constraint violation.
Basically the problem is that the user.getId property does not seem to get recognised and passed to the sql.
I have also noticed that if i search (HQL) on any field mapped via the association it never returns anything. Even if the search is valid.
Please see below...
Hibernate version: 3.2.2
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.jafp.objects.cms">
<class name="ContentItem" table="CMS_ContentItem">
<cache usage="read-write"/>
<id name="id" column="id" type="integer" unsaved-value="0">
<generator class="native"/>
</id>
<property name="active"/>
<property name="status"/>
<property name="title"/>
<property name="description" type="text"/>
<property name="feature"/>
<property name="releaseDate"/>
<property name="createdDate"/>
<many-to-one name="createdBy" class="com.jafp.objects.forum.User" cascade="none"/>
<property name="modifiedDate"/>
<many-to-one name="modifiedBy" class="com.jafp.objects.forum.User" cascade="none"/>
<property name="threadId"/>
<property name="imageName"/>
<property name="thumbnailName"/>
<property name="comments"/>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.jafp.objects.forum">
<class name="User" table="vBulletin_user">
<id name="id" column="userid" type="integer" unsaved-value="0">
<generator class="native"/>
</id>
<property name="memberGroupIds" column="membergroupids"/>
<property name="userGroupId" column="usergroupid"/>
<property name="username" column="username"/>
<property name="email" column="email"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
User user = getHibernateTemplate().load(User.class, 1);
Company owner = new Company();
owner.setTitle(OWNERS[i]);
owner.setCreatedDate(createdDate);
owner.setCreatedBy(owner);
owner.setModifiedDate(createdDate);
owner.setModifiedBy(owner);
hibernateDao.saveOrUpdate(owner);
getHibernateTemplate().saveOrUpdate(object);
Full stack trace of any exception that occurs:
Hibernate: insert into CMS_ContentItem (active, status, title, description, feature, releaseDate, createdDate, createdBy, modifiedDate, modifiedBy, threadId, imageName, thumbnailName, comments, itemType) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2007-03-27 23:16:22,656 WARN httpWorkerThread-8141-0 - SQL Error: 1216, SQLState: 23000
2007-03-27 23:16:22,671 ERROR httpWorkerThread-8141-0 - Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"
ApplicationDispatcher[/Web] Servlet.service() for servlet jsp threw exception
org.hibernate.exception.ConstraintViolationException: could not insert: [com.jafp.objects.cms.Company]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
Name and version of the database you are using:
MySql 4.1.22
_________________ _________________________
http://www.joshandfriends.com.au
Dance Music | Events | DJs
|