java - org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing -


i'm using spring boot , and spring data in project , have 2 classes:

class mission implements serializable { private static final long  serialversionuid = 1l; @id @generatedvalue( strategy = generationtype.identity ) private long              id; private string            departure; private string            arrival; private boolean           isfreewayenabled; @onetomany( mappedby = "mission" ) private list<station>     stations; // getters , setters } 

and second class :

@entity public class station implements serializable {  private static final long serialversionuid = 1l;  @id @generatedvalue( strategy = generationtype.identity ) private long              id; private string            station;  @manytoone( fetch = fetchtype.lazy ) @jsonbackreference private mission           mission;  //getters , setters  } 

methode add mission:

public mission addmision( mission mission ) {     // todo auto-generated method stub     // mission mission = getmissionbyid( mission.getid() );     ( station station : mission.getstations() ) {         station.setmission( mission );         stationrepository.save( station );     }     return missionrepository.save( mission ); } 

when tried add new mission gives error above heres json object :

{"departure":"fff","arrival":"ffff","isfreewayenabled":false,"stations":[{"id":1},{"id":2}]}

mession not persisted, need either save or cascade in station, have bidirectional relation, means each part of relation should have reference other part in mission class initialize station list

@onetomany( mappedby = "mission" ) private list<station> stations = new arraylist<>(); 

add cascade in station

@manytoone( fetch = fetchtype.lazy, cascade=cascadetype.all ) @jsonbackreference  private mission           mission; 

then in add logic put reference of each part of relation other

public mission addmision( mission mission ) {   list<station> stations = mission.getstations();  mission.setstations(new arraylist<station>());  ( station station : stations ) {     station.setmission( mission );     mission.getstations.add(station);     mission = stationrepository.save( station ).getmission(); }  return mission; } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -