-->
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.  [ 3 posts ] 
Author Message
 Post subject: Please help: many-to-one Duplicate key or integrity constrai
PostPosted: Tue Mar 27, 2007 9:31 am 
Newbie

Joined: Tue Dec 12, 2006 6:35 pm
Posts: 19
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


Top
 Profile  
 
 Post subject: Template?
PostPosted: Tue Mar 27, 2007 9:51 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
One problem is using the Hibernate template :-)

Apart from that, what is your hibernateDao doing?
Maybe this is not really al that is happening between open and close of the session (Doesn't spring's HibernateTemplate open/close a session if no transaction is active? I didn't bother to check the Spring sources, it doesn't say if it does/doesn't in the API docs.)
Also, you call setModifiedBy with the company instance (not the user). This cannot be the real code right?

Also, your hashCode and equals might be wrong.

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 7:36 pm 
Newbie

Joined: Tue Dec 12, 2006 6:35 pm
Posts: 19
Yes your correct that was dummy code.

Ok i've got to the bottom of this but am unsure what to do to solve it:


The problem is:

1) Schema export is putting a foreign key constraint on the createdBy and modifiedBy columns.

2) I then drop the vbulletin_user table and create it according to vbulletin forum software (this breaks the constraint)

3) When i execute even a native sql insert on the cms_contentitem table it will fail. Even if the correct createdBy value is set.



The question therefore is.... Is their a way to prevent Schema export adding a constraint for this foreign key??

_________________
_________________________
http://www.joshandfriends.com.au

Dance Music | Events | DJs


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