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