0
Good afternoon!
I am making a web service that receives a Json from the application, disembowels and inserts into the database. I can disembowel normally, the problem occurs at the time of entering the data in the database.
I have a list inside an object and at the time of inserting, it inserts several, instead of just one object and take advantage of the key to insert the data from the list.
For example, inside the json sent by the user, has a list of rooms of an immovable, when to insert these rooms, it repeats everything else.
Json received:
`{"email":"[email protected]","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 where I receive json and 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();
for (Endereco endereco : usuario.getEnderecoList()) {
comodoPersist = new Comodos();
for (Imovel imovel : endereco.getImovelList()) {
for (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);
}
}
Comodorepository class
public class ComodoRepository extends GenericDAO<Comodos> {
public ComodoRepository() {
super(Comodos.class);
}
}
Classe 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 was able to reach the solution by inserting the entity Imovel first, recovering the id and setting in the entity Comfortable for the insertion of the same separately.
for (Endereco endereco : usuario.getEnderecoList()) {
comodoPersist = new Comodos();
for (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());
for (Comodos comodo : imovel.getComodosList()) {
comodoPersist.setNome(comodo.getNome());
comodoPersist.setImovelId(imovelId);
comodoRepository.salvar(comodoPersist);
}
}
}
I also added the findById method in the 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;
}
I guess compacting won’t solve my problem.
– Rafael Carlos
I edited my question with the solution.
– Rafael Carlos