-->
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.  [ 4 posts ] 
Author Message
 Post subject: Many-to-one delete problem
PostPosted: Tue Nov 07, 2006 5:56 pm 
Newbie

Joined: Tue Sep 12, 2006 6:06 pm
Posts: 15
I'm having problems with a many-to-one relationship. I have an organization object that is associated with many resources. The association is bidirectional. Here is the mapping:

Organization:
Code:
   /**
    * @hibernate.set lazy="false"
    *       cascade="none"
    *      table="resource"
    *      inverse="true"
    *
    * @hibernate.collection-key column="organization_key"
    *
    * @hibernate.collection-one-to-many class="xxxx.domain.Resource"
    */
   public Set getResources() {


Resource:
Code:
   /**
    * @hibernate.many-to-one column="organization_key" not-null="false"
    *                        access="field"
    */
   public Organization getOrganization() {


Save/update seems to work correctly. The resource table's organization_key column is populated correctly. However, I am encountering a problem when I try to delete the resource. My delete code is as follows:

Code:
   public void delete(Resource resource) {
       getHibernateTemplate().delete(resource);
       getHibernateTemplate().flush();
   }


This yields the following error when executing the flush(). Although the organization field of the Resource is not null, the error tells me that the not-null column organization_key column is null.
Code:
2006-11-07 15:24:10,750 [main] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 0, SQLState: 01004
2006-11-07 15:24:10,750 [main] ERROR org.hibernate.util.JDBCExceptionReporter  - Data truncation: Column set to default value; NULL supplied to NOT NULL column 'organization_key' at row 1
2006-11-07 15:24:10,750 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener  - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
   at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:138)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
   at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:815)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
   at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:813)
   at com.silversky.scheduler.dao.organization.AbstractOrganizationDaoImpl.flush(AbstractOrganizationDaoImpl.java:87)
   at com.silversky.scheduler.dao.organization.AbstractOrganizationDaoImpl.delete(AbstractOrganizationDaoImpl.java:82)


Any ideas on the cause of this error?


Top
 Profile  
 
 Post subject: Not updating hibernate set may cause this
PostPosted: Tue Nov 07, 2006 6:19 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
It has been a while, but we had problems when we would naively reset a collection :

Code:
org1.setResources(someNewSet);


which would confuse hibernate. Instead we now use

Code:
org1.getResources().clearAll();
org1.getResources().addAll(someNewSet);


The issue is that HIbernate seems to insist on having us go through it's persistent collection implementations in order to do the right thing with deletes/orphans, etc.

As I said, it's been a while since we hit this so not sure if it is your issue.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 6:28 pm 
Newbie

Joined: Tue Sep 12, 2006 6:06 pm
Posts: 15
Sorry, there was an error in my first post. I am trying to delete the Organization, which is the parent object.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 7:26 pm 
Newbie

Joined: Tue Sep 12, 2006 6:06 pm
Posts: 15
Oddly, I get the same results even when I remove the not-null constraint off the database column and the hibernate mappings. Is a not-null constraint automatic for a foreign key?


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