spring - Nested exception is javax.persistence.EntityNotFoundException -


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 :

"unable find com.carpooling.entity.station id 2; nested exception javax.persistence.entitynotfoundexception: unable find com.carpooling.entity.station id 2"

heres json object sent:

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

you may need change little bit.

if use @manytoone, referenced entity must exist. otherwise specify field long have have , retrieve referenced entity means of separate query.

it throws exception (javax.persistence.entitynotfoundexception) instead of returning null if can't find requested entity.

use @notfound annotation resolve exception if lazy loading , not handling exception manually. have given syntax ..

@manytoone(     fetch = fetchtype.lazy) @notfound(     action = notfoundaction.ignore) @joincolumn(     name = column,     referencedcolumnname = column,     insertable = false,     updatable = false) private mission mission; 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -