EclipseLink and JPA

1

I have problems trying to write information to the database, in a simple WS with JPA, EclipseLink and Postgresql database.

I can fetch data and display it as json in the browser with GET methods, however I am also presetting to write data (POST) and at the moment you receive error message:

Info: [EL Info]: 2017-01-04 09:12:44.568--ServerSession(1619327197)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
Info: [EL Info]: connection: 2017-01-04 09:12:45.542--ServerSession(1619327197)--file:/D:/Documents/Projetos NetBeans/app/target/app/WEB-INF/classes/_persist-unit login successful
Severe: javax.persistence.TransactionRequiredException: 
Exception Description: No transaction is currently active
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.throwCheckTransactionFailedException(EntityTransactionWrapper.java:87)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.checkForTransaction(EntityTransactionWrapper.java:50)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:2041)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:863)
at br.com.bigfarma.app.service.ProductService.addProduct(ProductService.java:32)
at br.com.bigfarma.app.resourse.ProductResource.addProduct(ProductResource.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)...

PRODUCTSERVICE:

package br.com.bigfarma.app.service;

import br.com.bigfarma.app.entity.TbProduto;
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;

public class ProductService extends AbstractEntityManager {

public Collection<TbProduto> getAllProducts() {
    return getEm().createQuery(TbProduto.BASE_QUERY).getResultList();
}

public TbProduto addProduct(TbProduto tbProduto) {
    EntityManager em = getEm();
    em.setFlushMode(FlushModeType.COMMIT);
    EntityTransaction et = getEm().getTransaction();
    try {
        et.begin();
        if(!em.contains(tbProduto)){
            em.persist(tbProduto);
            em.flush(); //LOCAL DO ERRO
        }
        et.commit();
        return tbProduto;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}

IDE is Netbeans 8.1 with GlassFish 4.0 and Jersey 2.25

    
asked by anonymous 05.01.2017 / 12:57

1 answer

0

I solved the problem myself, it was just my lack of attention in my addProduct method I was doing this: EntityManager in = getEm (); em.setFlushMode (FlushModeType.COMMIT); EntityTransaction et = getEm () .getTransaction (); That is, I was setting up an EntityManager (in) but taking the transaction from a new entity manager (getEm). Another thing I did was also replace the em.flush () with em.merge () and remove the em.setFlushMode that was not serving for anything, so I was able to insert data without problem!

Code updates from addProduct method:

public TbProduto addProduct(TbProduto tbProduto) {
    EntityManager em = getEm();
    EntityTransaction transaction = em.getTransaction();
    try {
        transaction.begin();
        if (!em.contains(tbProduto)) {
            em.persist(tbProduto);
            em.merge(tbProduto);
        }
        transaction.commit();
        return tbProduto;
    } catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();
    }
    return null;
}

Big hug.

    
07.01.2017 / 13:52