java - How to Insert List with JPA -


i'm doing web service gets json app, uncapsules , inserts database. can decode normally, problem occurs @ time of inserting data database.

i have list inside object , @ time of inserting it inserts several instead of object , seize key insert data list.

for example, inside json commanded user, has list of rooms of property, when inserting these rooms, repeats else.

json received:

{"email":"teste@email.com","enderecolist":[{"bairrosetor":"centro","cep":"877777770","cidade":"palmas","complemento":"sn","estado":"to","imovellist":[{"comodoslist":[{"nome":"garagem - demoliçao de muro"},{"nome":"garagem - construção de muro"},{"nome":"garagem - regularização de contrapiso"},{"nome":"Área externa - demoliçao de muro"},{"nome":"Área externa - construção de muro"},{"nome":"Área externa - regularização de contrapiso"}],"tipo":"casa"}],"ruaalameda":"rua 1"}],"horario":"tarde","nome":"teste","telefone":"(63) 98889-2200"} 

methods json , insert

    @post     @path("inserir")     @produces({mediatype.application_json})     @consumes({mediatype.application_json})     public void add(string content) {          gson g = new gson();          usuario usuario = (usuario) g.fromjson(content, usuario.class);          usuario usuariopersist = new usuario();         endereco enderecopersist = new endereco();         imovel imovelpersist = new imovel();         comodos comodopersist = new comodos();          (endereco endereco : usuario.getenderecolist()) {             comodopersist = new comodos();             (imovel imovel : endereco.getimovellist()) {                 (comodos comodo : imovel.getcomodoslist()) {                      usuariopersist.setemail(usuario.getemail());                     usuariopersist.sethorario(usuario.gethorario());                     usuariopersist.setnome(usuario.getnome());                     usuariopersist.settelefone(usuario.gettelefone());                      enderecopersist.setbairrosetor(endereco.getbairrosetor());                     enderecopersist.setcep(endereco.getcep());                     enderecopersist.setcidade(endereco.getcidade());                     enderecopersist.setcomplemento(endereco.getcomplemento());                     enderecopersist.setestado(endereco.getestado());                     enderecopersist.setnumero(endereco.getnumero());                     comodopersist.setnome(comodo.getnome()); //                    comodos.add(comodopersist);                     imovelpersist.settipo(imovel.gettipo());                     enderecopersist.setruaalameda(endereco.getruaalameda());                      enderecopersist.setusuarioid(usuariopersist);                     imovelpersist.setenderecoid(enderecopersist);                     comodopersist.setimovelid(imovelpersist);                      new comodorepository().save(comodopersist);                 }              } 

class comodorepository

 public class comodorepository extends genericdao<comodos> {      public comodorepository() {         super(comodos.class);     }  } 

class genericdao

    public abstract class genericdao<t extends serializable> {      private class<t> aclass;      protected genericdao(class<t> aclass) {         this.aclass = aclass;         this.log = logmanager.getlogger(aclass.getname());;     }      protected entitymanager getentitymanager() {         return jpautil.getinstance().getentitymanager();     }         public void save(t entity) {         entitymanager manager = getentitymanager();         manager.gettransaction().begin();         manager.persist(entity);         manager.gettransaction().commit();         manager.close();     } } 

solution

i able reach solution first inserting imovel entity, retrieving id , setting on comodos entity insert separately.

for (endereco endereco : usuario.getenderecolist()) {         comodopersist = new comodos();         (imovel imovel : endereco.getimovellist()) {             usuariopersist.setemail(usuario.getemail());             usuariopersist.sethorario(usuario.gethorario());             usuariopersist.setnome(usuario.getnome());             usuariopersist.settelefone(usuario.gettelefone());              enderecopersist.setbairrosetor(endereco.getbairrosetor());             enderecopersist.setcep(endereco.getcep());             enderecopersist.setcidade(endereco.getcidade());             enderecopersist.setcomplemento(endereco.getcomplemento());             enderecopersist.setestado(endereco.getestado());             enderecopersist.setnumero(endereco.getnumero());              imovelpersist.settipo(imovel.gettipo());             enderecopersist.setruaalameda(endereco.getruaalameda());              enderecopersist.setusuarioid(usuariopersist);             imovelpersist.setenderecoid(enderecopersist);              imovelrepository.salvar(imovelpersist);             imovel imovelid = new imovelrepository().findbyid(imovelpersist.getid());              (comodos comodo : imovel.getcomodoslist()) {                  comodopersist.setnome(comodo.getnome());                 comodopersist.setimovelid(imovelid);                 comodorepository.salvar(comodopersist);             }          }      } 

i added findbyid method in genericdao class.

    public t findbyid(integer id) {     entitymanager manager = getentitymanager();     manager.gettransaction().begin();      t entity = (t) manager.find(aclass, id);      manager.gettransaction().commit();     manager.close();      return entity; } 


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -