I have following entities:
@Entity
@Table(name="APLICACAO")
public class Aplicacao implements Serializable, Entidade {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="CD_APLICACAO")
private Long codigo;
@Column(name="NM_APLICACAO")
@NotNull
private String nome;
@ManyToOne
@JoinColumn(name="CD_GRUPO")
private GrupoAplicacao grupoAplicacao;
....
}
And also:
@Entity
@Table(name = "GRUPO_APLICACAO")
public class GrupoAplicacao implements Serializable, Entidade {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CD_GRUPO")
private Long codigo;
@Column(name = "NM_GRUPO")
@NotNull
private String nome;
@OneToMany(mappedBy = "grupoAplicacao",fetch=FetchType.EAGER)
private List<Aplicacao> listaAplicacao;
@OneToMany(mappedBy = "grupoAplicacao",fetch=FetchType.EAGER)
private List<GrupoAplicacaoUsuario> listaGrupoAplicacaoUsuario;
....
}
My HTML call controller "aplicacoesCreateController", like code bellow:
<div class="col-xs-12" ng-controller="aplicacoesCreateController">
<form class="form-horizontal form-medium center" role="form" name="form" ng-submit="save()">
<div class="form-group">
<label for="nome" class="col-xs-4 control-label">Nome</label>
<div class="col-xs-8">
<input type="text" class="form-control input-sm" id="nome" placeholder="Nome" required ng-model="aplicacao.nome">
</div>
</div>
<div class="form-group">
<label for="nome" class="col-xs-4 control-label">Nome</label>
<div class="col-xs-8">
<select class="form-control" required ng-model="aplicacao.grupoAplicacao">
<option ng-repeat="grupo in grupos" value="{{ grupo }}">
{{ grupo.nome }}
</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-4 col-xs-8">
<button type="submit" class="btn btn-lg btn-size-md">Salvar</button>
</div>
</div>
</form>
</div>
And my Controller JavaScript:
app.controller('aplicacoesCreateController', ['$scope','aplicacoesService','$location','reloadService','gruposService',
function ($scope,aplicacoesService,$location,reloadService,gruposService) {
$scope.grupos = gruposService.list();
$scope.save = function () {
aplicacoesService.create($scope.aplicacao);
reloadService.on('#/aplicacoes');
};
}]);
And Services JavaScript:
app.factory('gruposService', ['$resource', function ($resource) {
return $resource('resources/grupo-aplicacao', {}, {
'list': { method: 'GET', isArray: true }
});
}]);
And other service:
app.factory('aplicacoesService', ['$resource', function ($resource) {
return $resource('resources/aplicacao', {}, {
'list': { method: 'GET', isArray: true },
'create': { method: 'POST' }
});
}]);
When insert aplicacao entity, is show me the following error:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class br.com.techpeople.grape.entity.GrupoAplicacao] from JSON String; no single-String constructor/factory method (through reference chain: br.com.techpeople.grape.entity.Aplicacao["grupoAplicacao"])
Could you help me with this error?
Once the task and the same issue was in scope of: +limited time, +ee: +jax-rs && +persistence, +gson; I have solved it then as:
It's pretty close to Gustavo's Bitencourt solution, with maybe a bit different scope.
It seems that, from javascript, you are sending a string instead of an object. You can try by sending
JSON.parse(stringGrupoAplicacao)
.That way, on the server side the object should be deserialized automatically with no needs of creating a special constructor.
The error is telling you that you need a constructor method in your GrupoAplicacao class which accepts a string.
Thank you @BoatCode!
I did the following in my constructor:
Add the lib Gson and set the variables of GrupoAplicacao class.
=)