-->
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: TransientObjectException HELP !
PostPosted: Fri Jul 21, 2006 10:35 am 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:03 pm
Posts: 22
Hibernate version:
NHibernate 1.2.0.Alpha1


Mapping documents:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="BLL" namespace="BLL">
   <class name="Movies" table="tbl_movies">

      <id name="Id" column="id_movie" type="Int32" unsaved-value="any">
      <generator class="sequence">
        <param name="sequence">movie_id_seq</param>
      </generator>
      </id>
<bag name="lImg" table="tbl_img" access="field" inverse="false" lazy="true" cascade="all">
      <key column="id_movie"/>
      <one-to-many class="BLL.Image,BLL"/>
    </bag>


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="BLL" namespace="BLL">
   <class name="Image" table="tbl_img">

      <id name="Id" column="id" type="Int32" unsaved-value="any">
      <generator class="sequence">
        <param name="sequence">img_id_seq</param>
      </generator>
      </id>
      <property column="link" type="String" name="Link" length="100" />

    <many-to-one name="Movie" class="BLL.Movie,BLL" column="id_movie"  />
  </class>
</hibernate-mapping>





My code
[code]
Movie class
public class Movies
{

private int id;
private IList<Imagens> lImagem;
...
}

Image class
public class Image
{
private int id;
private Movies movie;
...
}

-------------
Movie _movie = new Movie();
Ilist<Image> _list = new List<Image>;
Image _img = new Image();
_img.Link = "www.test.com"
_list.Add(_img);

_movie.lImg = _list;

ITransaction _tx = session.BeginTransaction();
session.SaveOrUpdate(_movie);
_tx.Commit(); <---- Error


ERROR
object references an unsaved transient instance - save the transient instance before flushing: BLL.Movies

[TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: BLL.Movies]
DAL.BaseDataAccess.Save(Object __item, Boolean __pointlessParameter) in D:\Util\Reps\MovieDB\trunk\MovieDB\DAL\BaseDataAccess.cs:113
DAL.BaseDataAccess.Save(BusinessBase __item) in D:\Util\Reps\MovieDB\trunk\MovieDB\DAL\BaseDataAccess.cs:90
Facade.FilmesFacade.Save(Movies__movie) in D:\Util\Reps\MovieDB\trunk\MovieDB\Facade\FilmesFacade.cs:28
Admin_Filmes_WFCadastro.btnIncluir_Click(Object sender, EventArgs e) in d:\Util\Reps\MovieDB\trunk\MovieDB\Web\Admin\Filmes\WFCadastro.aspx.cs:67
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 11:51 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
you will need to add a <version> property to your Image class. SaveOrUpdate() relies upon the version property to determine if the object has "changed" or not. so you would end up with:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="BLL" namespace="BLL">
   <class name="Image" table="tbl_img">

      <id name="Id" column="id" type="Int32" unsaved-value="any">
      <generator class="sequence">
        <param name="sequence">img_id_seq</param>
      </generator>
      </id>
       <version column="Version" type="Int32" unsaved-value="-1" />


as an example using Int32s, Longs or DateTime (Timestamps) are also possible. See doco for more info. A note: the version property must appear immediately after the <id> property.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 12:22 pm 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:03 pm
Posts: 22
devonl wrote:
you will need to add a <version> property to your Image class. SaveOrUpdate() relies upon the version property to determine if the object has "changed" or not. so you would end up with:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="BLL" namespace="BLL">
   <class name="Image" table="tbl_img">

      <id name="Id" column="id" type="Int32" unsaved-value="any">
      <generator class="sequence">
        <param name="sequence">img_id_seq</param>
      </generator>
      </id>
       <version column="Version" type="Int32" unsaved-value="-1" />


as an example using Int32s, Longs or DateTime (Timestamps) are also possible. See doco for more info. A note: the version property must appear immediately after the <id> property.

-devon



Thanks, but I got the same error... :(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 6:04 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
d'oh! I wasn't looking at your code very closely. While I am not yet working with Generics in .NET, I think your problem is here:

Code:
_movie.lImg = _list;


With NH (and Hibernate) you can't assign a collection this way. You need to use the Add() method of the collection type to add each instance. So something like:

Code:
Movie _movie = new Movie();

Image _img = new Image();
_img.Link = "www.test.com";

_movie.lImagem.Add(_img);

ITransaction _tx = session.BeginTransaction();
session.SaveOrUpdate(_movie);
_tx.Commit();


Sorry about the confusion. But my original note still stands. You must use <version> property with SaveOrUpdate().

-devon


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.