-->
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: Solve TransientObjectException without cascade.
PostPosted: Mon Aug 07, 2017 10:36 am 
Newbie

Joined: Thu Aug 03, 2017 4:02 am
Posts: 12
Hi @all,

my model contains a document and a file object. It is possible, that several documents point to one file, so it´s a manyToOne relation from document to file. Here the relevant parts from my document class:

Code:
@Entity
@org.hibernate.annotations.Proxy(lazy = true)
@Table(name = "`DOCUMENT`", schema = "MYSCHEME")
@org.hibernate.search.annotations.Indexed
@org.hibernate.search.annotations.Analyzer(impl = MyAnalyzer.class)
public class Document{
...
  @ManyToOne(targetEntity = Fileh.class, fetch = FetchType.LAZY)
  @JoinColumns({ @JoinColumn(name = "`FILEHID`", referencedColumnName = "`ID`") })
  private File fileh;

...
}


The file has no idea of the existence of a document so there´s no link.

If I now create and save a document and a file, the following exception is thrown:
Code:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: File


Okay, the exception is clear, but due to my coding I thought I saved the file before:

Code:
File file = new File();
Document document = new Document();
document.setFile(file);

PersistentTransaction transaction = session.beginTransaction();
session.saveOrUpdate(file); // <- shouldn´t this be the solution of my problem?
session.saveOrUpdate(document);
transaction.commit();


Sure I could solve the problem by adding the following cascade, but I will avoid it if possible:
Code:
@org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.LOCK })
  @ManyToOne(targetEntity = Fileh.class, fetch = FetchType.LAZY)
  @JoinColumns({ @JoinColumn(name = "`FILEHID`", referencedColumnName = "`ID`") })
  private File fileh;


Do you have any suggestions?
Thanks in advance and regards,
Vincent


Top
 Profile  
 
 Post subject: Re: Solve TransientObjectException without cascade.
PostPosted: Sun Aug 13, 2017 2:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
session.saveOrUpdate(file); // <- shouldn´t this be the solution of my problem?


Yes, it should.

Cascades on @ManyToOne make no sense, so avoid them.


Top
 Profile  
 
 Post subject: Re: Solve TransientObjectException without cascade.
PostPosted: Tue Aug 22, 2017 11:10 am 
Newbie

Joined: Thu Aug 03, 2017 4:02 am
Posts: 12
But unfortunately it doesn´t solve my problem - at the moment only @ManyToOne Cascading works :(


Top
 Profile  
 
 Post subject: Re: Solve TransientObjectException without cascade.
PostPosted: Wed Aug 23, 2017 5:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That cannot be true. Try to write a replicating test case using our templates and prove that saving these two entities explicitly does not work as expected.


Top
 Profile  
 
 Post subject: Re: Solve TransientObjectException without cascade.
PostPosted: Fri Aug 25, 2017 7:44 am 
Newbie

Joined: Thu Aug 03, 2017 4:02 am
Posts: 12
Problem is solved - one of the documents in my list had a linked fileh that was not saved before. Didn´t recognize that, because all other fileheaders where there. My fault :(
Thank you for your patience Vlad!


Top
 Profile  
 
 Post subject: Re: Solve TransientObjectException without cascade.
PostPosted: Fri Aug 25, 2017 8:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You're welcome.


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.