problem summary:
i have 3 @entities following relationship: customerorder -> @onetoone -> reservation -> @onetomany -> productorder.
when call controller fetch customerorder directly, customerorder , reservation child object pulled. list<productorder>
remains empty.
if call controller fetch reservation directly, list<productorder>
under reservation populated.
when print out sql hibernate generates fetch list<productorder>
, sql in fact pull appropriate productorder list spring not populating list.
details:
i have 3 classes (getters , setters omitted brevity):
customerorder:
@entity public class customerorder { @id @generatedvalue(strategy = generationtype.auto) private long id; @onetoone(fetch = fetchtype.eager) @joincolumn( name="id",referencedcolumnname="customerorderid",insertable=false, updatable=false) private reservation reservation; }
reservation
@entity public class reservation { @id @generatedvalue(strategy = generationtype.auto) private long id; //tie reservation customer order private long customerorderid; @onetomany @joincolumn(name = "reservationid", referencedcolumnname="id", insertable=false, updatable=false) private list<productorder> productorderlist = new arraylist<productorder>();
}
product order:
@entity public class productorder { @id @generatedvalue(strategy = generationtype.auto) private long id; private long customerorderid; private long reservationid; }
i have 2 endpoints. 1 can fetch reservation directly, or 1 can fetch reservation via customerorder:
@requestmapping(value = "/customer-order/{id}", method = requestmethod.get) public @responsebody customerorder findone(@pathvariable(value="id") long id) { return customerorder customerorder = customerorderrepository.findone(id); } @requestmapping(value = "/reservation/{id}", method = requestmethod.get) public @responsebody reservation findone(@pathvariable(value="id") long id) { return reservationservice.findone(id); }
i using spring-boot version 1.3.5.release mysql 5.6.23
i figured out going on.
initially had reservation have @onetomany relationship customerorder. meant mapping should used this:
@onetomany @joincolumn( name="customerorderid",referencedcolumnname="id",insertable=false, updatable=false) private list<reservation> reservation;
i changed code use @onetoone mapping. when did flipped join column this: @joincolumn(name="id",referencedcolumnname="customerorderid",insertable=false, updatable=false)
this action wrong. when getting reservation object via customerorder object through mapping, getting wrong reservation object because of incorrect @joincolumn mapping.
the correct solution modify customerorder @entity have id references reservation.
my customerorder class looks this:
private long reservationid; @onetoone @joincolumn( name="reservationid",referencedcolumnname="id",insertable=false, updatable=false) private reservation reservation;
woops.