java - @OneToOne Relationship on Spring mcv -


i'm tired of trying things , don't have results. user has 1 country , country has many user. problem when make new user. don't have idea problem.

i have user bean:

@entity @table(name="usuarios") public class usuario {      @id     @generatedvalue     @column(name="id")     private integer id;      @column(name="nombre", nullable=false)     private string nombre;      @onetoone     @joincolumn(name = "id_pais")     private pais pais;      /* getters & setters */     .... } 

the country bean:

@entity @table(name="paises") public class pais {      @id     @generatedvalue(strategy = generationtype.auto)     @column(name="id")     private integer id;      @column(name="nombre")     private string nombre;      @onetomany(mappedby="pais")     private set<usuario> usuarios;      /* getters & setters */  } 

the controller:

@controller @requestmapping(value = "/usuario") public class usuarioscontroller {      @autowired     private usuarioservice usuarioservice;      @requestmapping(method = requestmethod.post)     public redirectview store(usuario usuario) {         usuarioservice.store(usuario);               return new redirectview("../usuario");     }     @modelattribute("usuario")     public usuario getusuario() {         return new usuario();     }    } 

the service:

@service public class usuarioserviceimplement implements usuarioservice{      @autowired     private usuariodao usuariodao;      public void store(final usuario usuario) {         usuariodao.store(usuario);     } } 

the dao:

@repository public class usuariohibernatedao implements usuariodao{      @autowired     private sessionfactory sessionfactory;      @transactional(readonly = false)     public void store(final usuario usuario) {         sessionfactory.getcurrentsession().save(usuario);     } } 

the form:

<form:form action="../usuario" method="post" commandname="usuario">      <!-- nombre -->     <form:input path="nombre" type="text" class="form-control" id="nombre" />      <!-- pais -->      <form:select path="pais" class="form-control">         <form:option value="1">españa</form:option>     </form:select>     <input type="submit" class="btn btn-success" value="enviar" /> </form:form> 

and obtain this:

estado http 400 -  type informe de estado  mensaje  descripción el requerimiento enviado por el cliente era sintácticamente incorrecto.  apache tomcat/8.0.20 

i had same problem these,you can set object manually id.i hope solve problem.

try this!

usuario.setpais(paisservice.getpaisbyid(request.getparameter("pais"))); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -