I have a problem in which I try to persist data in the Bank 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.
Follow below as 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 Craftsman object is not persisted.
I made a change to 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){
}
}
It has 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 Master 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();
}
}
}