-->
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.  [ 2 posts ] 
Author Message
 Post subject: CascadeType.PERSIST problem
PostPosted: Thu Oct 13, 2005 8:53 am 
Newbie

Joined: Tue Sep 20, 2005 10:13 am
Posts: 5
Sorry for my bad english :o)

First class

Code:
package testcascade;

import java.io.Serializable;
import javax.persistence.*;

@Entity(access=AccessType.FIELD)
@Table(name="CHILD")
public class Child {
@Id(generate=GeneratorType.AUTO)
protected Long id;
   
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}

@ManyToOne(cascade=CascadeType.PERSIST, fetch=FetchType.LAZY)
protected Parent p;
   
public Parent getParent(){return p;}
public void setParent(Parent p) {   this.p = p;   }
}


second class

Code:
package testcascade;
import javax.persistence.*;

@Entity(access=AccessType.FIELD)
@Table(name="PARENT")
public class Parent{

@Id(generate=GeneratorType.AUTO)
protected Long id;

public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
}



test class

Code:
package testcascade;
import org.hibernate.*;
import test.HibernateUtil2;

public class TestCascade {
public static void main(String[] args) {
Parent p = new Parent();
Child c = new Child();
c.setParent(p);
   
Session s = HibernateUtil2.currentSession();
Transaction tx = s.beginTransaction();
       
s.saveOrUpdate(c);
   
tx.commit();

tx = s.beginTransaction();
s.delete(c);
tx.commit();
HibernateUtil2.closeSession();
}
}



Create Child, create Parent, set Parent to Child, save Child. Parent also should be saved because CascadeType.PERSIST.
But sql-log is INSERT INTO CHILD VALUES(1,NULL) and exception throws
Code:
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: testcascade.Parent


Looks like PERSIST (and other cascade types but ALL doesn't work).
When I set CascadeType.ALL - it works fine. But when delete Child - parent also delete (ALL :o)) I don't want this.

But if replace ALL with {MERGE, REFRESH, REMOVE, PERSIST} - same sql query and exception!

I use latest
hibernate-3.1.rc1.jar
hibernate-annotations-3.1beta5.jar
ejb3-persistence-1.0.jar

Help! :o)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 2:57 am 
Newbie

Joined: Tue Sep 20, 2005 10:13 am
Posts: 5
Fixed.

Use persist() instead of saveOrUpdate()


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