-->
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.  [ 8 posts ] 
Author Message
 Post subject: Persistence save fails under MySql
PostPosted: Sat May 30, 2009 11:50 am 
Newbie

Joined: Sat May 30, 2009 11:42 am
Posts: 15
Hi,

I might just be doing something dumb but since it only happens in a specific case i'm wondering if it is a bug:

I have a table called "Media" which has a mapping with "Selections"

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyApp.Data.BusinessObjects" assembly="MyApp.Data">
   <class name="MyApp.Data.BusinessObjects.Media, MyApp.Data" table="media" where="deleted = 0" lazy="true">
      <id name="Id" column="id">
          <generator class="assigned" />
      </id>
      <property name="Title" column="title" />
      <property name="Sabam" column="sabam" />
      <property name="Disc" column="disc" />
      <property name="Track" column="track" />
      <property name="Year" column="year" />
      <property name="Duration" column="duration" />
      <property name="Leadin" column="leadin" />
      <property name="Leadout" column="leadout" />
      <property name="Validfrom" column="validfrom" />
      <property name="Validuntil" column="validuntil" />
      <property name="Class" column="class" />
      <property name="Published" column="published" />
      <property name="Reccreated" column="reccreated" />
      <property name="Reccreater" column="reccreater" />
      <property name="Recmodified" column="recmodified" />
      <property name="Recmodifier" column="recmodifier" />
      <property name="Recdeleted" column="recdeleted" />
      <property name="Recdeleter" column="recdeleter" />
      <property name="Deleted" column="deleted" />
      <many-to-one name="MediaMember" column="childid" class="Media"/>
      <many-to-one name="File1" column="filedefault" class="File"/>
      <many-to-one name="File2" column="filesource" class="File"/>
      <many-to-one name="FileType" column="filetype" class="FileType"/>
      <many-to-one name="MediaGenre" column="genre" class="MediaGenre"/>
      <many-to-one name="Language" column="language" class="Language"/>
      <many-to-one name="Library" column="library" class="Library"/>
      <many-to-one name="MediaStatus" column="status" class="MediaStatus"/>
      <many-to-one name="MediaType" column="mediatype" class="MediaType"/>
      <many-to-one name="MediaVoice" column="voice" class="MediaVoice"/>
      <many-to-one name="Author" column="author" class="Author"/>
      <many-to-one name="Country" column="country" class="Country"/>
      <many-to-one name="MediaGender" column="gender" class="MediaGender"/>
      <many-to-one name="MediaId3genre" column="id3genre" class="MediaId3genre"/>
      <many-to-one name="MediaTempo" column="tempo" class="MediaTempo"/>
      <bag name="CollectionlistMediamembers" lazy="true" cascade="all-delete-orphan" inverse="true" where="deleted = 0" >
         <key column="media"></key>
         <one-to-many class="CollectionlistMediamember"></one-to-many>
      </bag>
      <bag name="Files" lazy="true" cascade="all-delete-orphan" inverse="true" where="deleted = 0" >
         <key column="media_id"></key>
         <one-to-many class="File"></one-to-many>
      </bag>
      <bag name="MediaCustomers" lazy="true" cascade="all-delete-orphan" inverse="true" where="deleted = 0" >
         <key column="media"></key>
         <one-to-many class="MediaCustomer"></one-to-many>
      </bag>
      <bag name="PlaylistMedia" lazy="true" cascade="all-delete-orphan" inverse="true" where="deleted = 0" >
         <key column="media"></key>
         <one-to-many class="PlaylistMedium"></one-to-many>
      </bag>
      <bag name="SelectionlistMediamembers" lazy="true" cascade="all-delete-orphan" inverse="true" where="deleted = 0" >
         <key column="media"></key>
         <one-to-many class="SelectionlistMediamember"></one-to-many>
      </bag>
      <sql-delete>update media set deleted = 1,recdeleted = now() where id = ?</sql-delete>
   </class>
</hibernate-mapping>


Now , when i create a new media, add selectionlistmediamembers to them, this is NOT saved in persitence:
Code:
Media oMedia = new Media();
oMedia.Id = Guid.NewGuid().tostring();
//fill out all other parameters here

Selectionlistmediamember oSelMem = new Selectionlistmediamember();
oSelMem.Id = Guid.newGuid().tostring();
//fill out other stuff

oMedia.SelectionlistMediaMembers.add(oSelMem);

oMediaManager.Save(oMedia);

/* At this point, i get an error saying:

nhibernate.stalestateexception unexpected row count 0 expected 1
*/




when NOT adding the selectionmediamembers, saving the media and THEN adding the members and updating, it does work. The other effort was trying to save the selectionmediamembers first but this cannot be done since the foreign key does not allow me to save items who's parent cannot be found (logically)

Am i doing something wrong here or is there an issue with the caching of the object / my mapping ?
Many thanks in advance !


Top
 Profile  
 
 Post subject: Re: Persistence save fails under MySql
PostPosted: Tue Jun 02, 2009 2:48 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Since you mapped the bag as inverse="true" you need a reference to media on member. Adding it to the collection is not enough:

Code:
oMedia.SelectionlistMediaMembers.add(oSelMem);
oSelMem.Media = oMedia;


Or is that hidden in the add Method on the member list ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Persistence save fails under MySql
PostPosted: Wed Jun 03, 2009 2:20 am 
Newbie

Joined: Sat May 30, 2009 11:42 am
Posts: 15
Hi !,

Thanks for your reply. Unfortunaltely, i've tryed that but it does not seems to work. When i try this and the save occurs, MySql gives me a foreign constrait error which leads me to believe the Selectionmember is saved BEFORE the media has been saved (not out app code though )..


Top
 Profile  
 
 Post subject: Re: Persistence save fails under MySql
PostPosted: Wed Jun 03, 2009 2:43 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post the mapping file of SelectionlistMediamember ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Persistence save fails under MySql
PostPosted: Wed Jun 03, 2009 2:49 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Wait, the reason for this behavior is probably because of the assigned ids:

http://nhforge.org/doc/nh/en/index.html#example-parentchild-update

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Persistence save fails under MySql
PostPosted: Wed Jun 03, 2009 5:09 am 
Newbie

Joined: Sat May 30, 2009 11:42 am
Posts: 15
I've read through this documentation and what i understand:

Since Nhibernate is not generating the Guids, it doesn't really know about it's parent. ( i have tryed saving the parent before saving the children but then the error occurs on the adding of the children to the parent's collection)

So.. the easiest way out here would be to let Nhibernate generate my Guids for me right ?

I'll give that a try but I do want to say thanks already !.. i completely read over this part :s

It might be a bit much to ask but i have a simular issue ( I Think ) with deleting children that i have posted here too.. (https://forum.hibernate.org/viewtopic.php?f=25&t=997266).

Could you be so kind to take a look at that ? I think it is related to this since when i do a logical delete ( custom <sql-delete> and <where="deleted = 0"> attribute on the class). It get's deleted but get's updated AFTERWARDS and seems to be reset to deleted = 0, i am thinking this is something with the cache no knowing about the object being deleted and re-saving it on the next commit() action :s

eithery way already thanks for you effort !



Many thanks in advance!


Top
 Profile  
 
 Post subject: Re: Persistence save fails under MySql
PostPosted: Wed Jun 03, 2009 5:10 am 
Newbie

Joined: Sat May 30, 2009 11:42 am
Posts: 15
P.S: i would love to rate your message but is it me or does this Forum miss that option ?


Top
 Profile  
 
 Post subject: Re: Persistence save fails under MySql
PostPosted: Wed Jun 03, 2009 5:28 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
It's not that hibernate does not know about the parents when you use assigned ids. Hibernate does not know if it has to insert or update the child, because there's nothing like an unsaved value with assigne ids.

_________________
--Wolfgang


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