java - hibernate - cascading type persist for many to many relationship -


i have 2 classes policy , acl. have manytomany relationship in them.

// policy

public class policyentity {     @id     @generatedvalue(strategy = generationtype.sequence)     @column(name = "policy_id")     private int policyid;        @column(name = "policy_name")     private string policyname;     @manytomany(cascade = cascadetype.persist)     @jointable(name = "policy_acl",joincolumns=@joincolumn(name = "policy_id"),                                     inversejoincolumns=@joincolumn(name = "acl_id"))     private list<aclentity> acllist = new arraylist<aclentity>(); } 

// acl

public class aclentity {     @id     @generatedvalue(strategy = generationtype.sequence)     @column(name = "acl_id")     private int aclid;       @column(name = "acl_name")     private string aclname;     @manytomany(mappedby = "acllist")     private list<policyentity> policylist = new arraylist<policyentity>(); } 

now when use cascade type persist got following error :

hibernate: insert policy (policy_name, policy_id) values (?, ?) hibernate: insert policy_acl (policy_id, acl_id) values (?, ?) exception in thread "main" org.hibernate.transientobjectexception: object references unsaved transient instance - save transient instance before flushing: hibernate.aclentity 

whereas when use cascade type saves policy entity , acl entity successfully.i unable understand how happening. note : performing save operation.

here main code :

public static void main(string[] args) {          policyentity policyentity = new policyentity();         aclentity aclentity = new aclentity();          //policyentity.setpolicyid(1);         policyentity.setpolicyname("policy 3");         //aclentity.setaclid(1);         aclentity.setaclname("acl 3");          policyentity.getacllist().add(aclentity);          configuration configuration = new configuration().configure();         standardserviceregistrybuilder builder = new standardserviceregistrybuilder().         applysettings(configuration.getproperties());         sessionfactory sessionfactory = configuration.buildsessionfactory(builder.build());         session session = sessionfactory.opensession();         session.begintransaction();          session.save(policyentity);          session.gettransaction().commit();         session.close();     } 

use session.persist() instead of session.save()

refer hibernate docs clear details:

1) session.persist():

make transient instance persistent. operation cascades associated instances if association mapped cascade="persist"

2) session.save():

persist given transient instance, first assigning generated identifier. (or using current value of identifier property if assigned generator used.) operation cascades associated instances if association mapped cascade="save-update"


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -