java - Can Jersey find a POJO from an ID when mapping from JSON? -


it's hard put words, i'll best.

high level: writing web service add new prblfldr entity database, need associate proper prbltmplt (please ignore terrible naming scheme, didn't it). posting json, jersey maps correct pojos. but, instead of calling pojo's constructor 1 of fields, want use entitymanager find correct object based on id.

low level: here's sample of json trying pass web service:

{     fldrnm: "test",     prbltmplt: {         tmpltseqid: 4     } } 

here's code on other side, web service sits:

@post @consumes(mediatype.application_json) @path("/folders/create") public response createfolder(prblfldr folder) {     em.persist(folder);     return response.ok(gson.tojson(folder), mediatype.application_json).build(); } 

now, here's prblfldr entity looks like, lot of irrelevant fields omitted:

@entity @table(name="prbl_fldr") @namedquery(name="prblfldr.findall", query="select p prblfldr p") public class prblfldr implements serializable {      @expose     @column(name="fldr_nm")     private string fldrnm;      //bi-directional many-to-one association prbltmplt     @expose     @manytoone     @joincolumn(name="fldr_typ_seq_id", insertable=false, updatable=false)     private prbltmplt prbltmplt;      // other fields... } 

see how prbltmplt field joined on fldr_typ_seq_id? need id object. lastly, here our prbltmplt class looks like, again relevant information included:

@entity @table(name="prbl_tmplt") @namedquery(name="prbltmplt.findall", query="select p prbltmplt p") public class prbltmplt implements serializable {      @expose     @serializedname("id")     @id     @column(name="tmplt_seq_id")     private long tmpltseqid;      // other fields... } 

the ultimate goal to prevent need creating intermediate pojo jersey maps to, turn right around fill out relevant fields in new prblfldr object (and use em.find() find prbltmplt we're using). i'm not sure if i'm talking possible, after searching hours, figured ask guys.

edit: more specify situation, above code (when log web service sends back) outputs json this:

{     fldrnm: "test",     prbltmplt: {         tmpltseqid: 4     } } 

... put in. instead, want tmpltseqid used lookup pk fetch object exists id in db, return might this:

{     fldrnm: "test",     prbltmplt: {         tmpltseqid: 4,         tmpltdesc: "sample description",         tmpltnm: "sample template name",         // etc...     } } 

i decided against original thought of having happen automatically. instead, created own "intermediate entity" pojos have allowed jaxb map to. perform own jpa lookups , build actual entity manually. looking back, more proper way since avoid ambiguity performing process hand.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -