JPA does not persist the Object in the bank

2

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();
        }


    }
}
    
asked by anonymous 17.06.2015 / 20:26

1 answer

1

My suggestions:

  

Inject DAO for it to be registered by the container and have the   dependencies injected.

@Controller
public class CadastroController {
    @Inject
    private Result result;
    @Inject
    private ArtesaoDAO artesaoDAO;       

    //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.salvar(artesao);
        } catch (Exception e) {
           e.printStackTrace();
        }

    }
}

In DAO:

  

Remembering that @Transactional anotation and so support for   @PersistenceContext injection by the container, outside an EJB only and   supported in JEE7 and JTA 1.2.

@Transactional(Transactional.TxType.REQUIRED)
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){

    }

}

About DAO that did not "commit":

  

In this case you would have had to get the userTransaction from the context (BMT   - Bean Managed Transaction) to have started and subsequently created the entity manager or made a entityManager.joinTransaction within   of the JTA transaction.

public class ArtesaoDAO {

public void salvar(Artesao artesao){
    EntityManager manager = ConnectionFactory.getEntityManager();
    ctx = new InitialContext();
    ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction");

    try {
        ut.begin();
        manager.joinTransaction();
        manager.persist(artesao);
        ut.commit();

    }catch(Exception e){
        ut.rollback();
    }finally{
        manager.close();
    }
}
    
18.06.2015 / 08:50