-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem with cascading persist/merge in one-to-many relation
PostPosted: Wed Jan 16, 2008 9:26 am 
Newbie

Joined: Wed Nov 21, 2007 8:16 am
Posts: 10
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.2.1.ga

Mapping documents:orm.xml and persistence.xml

Using Spring's JpaTemplate

Full stack trace of any exception that occurs: None

Name and version of the database you are using:MYSQL 5.0.45-community-nt

Hello
I have a problem when using one-to-many-relations where i have cascade-persist and cascade-merge


Mapping:
Code:
<entity class="domain.Incident" name="Incident">
   <!-- id and version is in a mapped superclass -->
   <attributes>
      <basic name="sensorId" />
      <one-to-many name="receivedMessages">
         <cascade>
            <cascade-persist />
            <cascade-merge />
         </cascade>
      </one-to-many>
   </attributes>
</entity>

<entity class="domain.ReceivedSMS" name="ReceivedSMS">
   <!-- id and version is in a mapped superclass -->
   <attributes>
      <basic name="senderCellphoneNumber" />
      <basic name="messageText" />
   </attributes>
</entity>


Domain objects:
Code:
public class Incident extends AbstractDomainObject {
   private Long sensorId;
   private Set<ReceivedSMS> receivedMessages = new
HashSet<ReceivedSMS>()
   
   public void setSensorId(Long sensorId){
      this.sensorId = sensorId;
   }

   public Long getSensorId(){
      return sensorId;
   }

   public void setReceivedMessages(Set<ReceivedSMS> messages){
      this.receivedMessages = messages;
   }

   public Set<ReceivedSMS> getReceivedMessages(){
      return receivedMessages;
   }

   // This is where the problem is
   public void addReceivedMessage(ReceivedSMS sms){
      receivedMessages.add(sms);
   }
}


Repository code for saving incidents:
Code:
public class RepositoryIml extends JpaTemplate implements Repository {
   public Incident save(Incident incident){
      if (incident.getId == null){
         persist(incident);
      } else {
         incident = merge(incident);
      }
       return incident;
   }
   ...
}


The ReceivedSMS instances are saved before they are added to the incident.

When i create a new Incident, and add a ReceivedSMS to it (before saving), everything works ok.

But when trying to add a ReceivedSMS to an already saved Incident, the ReceivedSMS is not bound to the incident in the relation table.

Do i do something wrong or is it a bug?

I'm quite new to Hibernate

/Per-Jarle


Top
 Profile  
 
 Post subject: Re: Problem with cascading persist/merge in one-to-many rela
PostPosted: Wed Jan 16, 2008 11:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Code:
public class RepositoryIml extends JpaTemplate implements Repository {
   public Incident save(Incident incident){
      if (incident.getId == null){
         persist(incident);
      } else {
         incident = merge(incident);
      }
       return incident;
   }
   ...
}


I think this should work, but I remember having the same problem. I added a em.flush() after the em.merge() and it seemed to work. Why I am not sure. I thought it should work without as well.

I would actually add another questions to yours. Is the if statement really needed? Do you have to check the id against null? Couldn't you not just call merge in both cases? If I understand the documentation correctly the if is not needed?
Quote:
The merge operation is clever enough to automatically detect whether the merging of the detached instance has to result in an insert or update.


(http://www.hibernate.org/hib_docs/entit ... le/#d0e902)

Is it performance hit to call merge instead of persist?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 3:26 am 
Newbie

Joined: Wed Nov 21, 2007 8:16 am
Posts: 10
I tried to add getEntityManager().flush() to the save method, but it didn't have any effect.

I changed my testcase so it should fail on this error, but the test is running ok.

My testcase extends AbstractJpaTests

/Per-Jarle


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 4:01 am 
Newbie

Joined: Wed Nov 21, 2007 8:16 am
Posts: 10
I also tried to remove the cascading.

Since the messages are saved before trying to add it to the Incident, cascading shouldn't be needed.

This had no effect

/Per-Jarle


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 6:51 am 
Newbie

Joined: Wed Nov 21, 2007 8:16 am
Posts: 10
For testing purposes i tried not to save the messages before they are added to the incidents.

This had a strange effect...

After saving the incidents, the messages were added to the received-messages table (because of the cascading). BUT they were not added to the relation table which map messages to incidents

I really need help on this

/Per-Jarle


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 9:35 am 
Newbie

Joined: Wed Nov 21, 2007 8:16 am
Posts: 10
I also tried to change from one-to-many to many-to-many relationships without any result

/Per-Jarle


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