JPA does not persist the Object in the database

Asked

Viewed 689 times

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


    }
}
  • 1

    No error is presenting, even if you print the stack trace of the exceptions you recover?

  • As Bruno said, print the error within the 2 catch (Null and Exception) this way it is easier to identify the problem

  • Thanks I’ll do that and see if he finds any exceptions.

  • 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 I’m using JTA.

  • What’s on line 43 CadastroController? It may be interesting to include the method cadastrar(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.

  • Line 43 craftsDAO.save(craftsman);

  • Okay, so artesaoDAO is null. You are probably using inversion control (isando @Inject). If so, how is activating the CDI container?

  • @Darlanoliveira does not edit the answer of users adding questions, edit the question.

Show 4 more comments

1 answer

2


My suggestions:

Inject the DAO so it can be registered by the container and have the injected dependencies.

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

    }
}

On the DAO:

Remembering that annotationtion @Transactional and so support for injection of @Persistencecontext by container, out of an EJB only and supported on 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 the DAO that didn’t "commit":

In that case you would have had to get the context userTransaction (BMT - Bean Managed Transaction) started and later 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();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.