2
I have a problem in which I try to persist data in the Database with JPA and Hibernate only that it does not persist the Object.
It creates the table but does not persist the data and no exception is presented.
Keep down like I’m doing:
public class ArtesaoDAO {
public void salvar(Artesao artesao){
// EntityTransaction transaction = manager.getTransaction();
EntityManager manager = ConnectionFactory.getEntityManager();
manager.getTransaction().begin();
try {
manager.persist(artesao);
manager.getTransaction().commit();
} catch (NullPointerException e) {
manager.getTransaction().rollback();
}catch(Exception e){
manager.getTransaction().rollback();
}finally{
manager.close();
}
}
As I said the table is created but the artisan object is not persisted.
I modified my DAO:
@Transactional
public class ArtesaoDAO{
@PersistenceContext(unitName = "portifoliows")
private EntityManager manager;
public void salvar(Artesao artesao){
System.out.print(artesao.getNome());
try {
this.manager.persist(artesao);
} catch (NullPointerException e) {
this.manager.getTransaction().rollback();
}catch(Exception e){
this.manager.getTransaction().rollback();
}
}
public void deletar(Artesao artesao){
}
}
He presents the following exception:
Grave: java.lang.NullPointerException
at com.ratossi.portifoliows.controllers.CadastroController.cadastrar(CadastroController.java:43)
at com.ratossi.portifoliows.controllers.CadastroController$Proxy$_$$_WeldClientProxy.cadastrar(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
My Register Controller:
@Controller
public class CadastroController {
@Inject
private Result result;
//Função para cadastar artesao
@Path("/cadastro")
@Post
public void cadastrar(Artesao artesao){
try {
result.include("artesao", "id:"+artesao.getId()+" Nome: "+artesao.getNome()+" Login:"+artesao.getLogin()+" Senha: "+artesao.getSenha());
System.out.print("Persistindo -- > "+"id:"+artesao.getId()+" Nome: "+artesao.getNome()+" Login:"+artesao.getLogin()+" Senha: "+artesao.getSenha());
ArtesaoDAO artesaoDAO = new ArtesaoDAO();
artesaoDAO.salvar(artesao);
} catch (Exception e) {
e.printStackTrace();
}
}
}
No error is presenting, even if you print the stack trace of the exceptions you recover?
– Bruno César
As Bruno said, print the error within the 2 catch (
Null
andException
) this way it is easier to identify the problem– Maicon Carraro
Thanks I’ll do that and see if he finds any exceptions.
– Darlan Oliveira
I found it strange how you do the transaction. A question: in your datasource, what type of transaction do you use: Resource_local or JTA? Another thing: Post also the entity mapped.
– Edgar Muniz Berlinck
Edgar I’m using JTA.
– Darlan Oliveira
What’s on line 43
CadastroController
? It may be interesting to include the methodcadastrar
(and relevant class attributes) to see what it is, apparently it’s not even getting to your DAO, so it might be some dependency injection problem in your controller.– Bruno César
Line 43 craftsDAO.save(craftsman);
– Darlan Oliveira
Okay, so
artesaoDAO
is null. You are probably using inversion control (isando@Inject
). If so, how is activating the CDI container?– Bruno César
@Darlanoliveira does not edit the answer of users adding questions, edit the question.
– Guilherme Nascimento