Eclipselink and JPA

Asked

Viewed 231 times

1

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

I can search data and present them as json in the browser with GET methods, but I must also write data (POST) and at this time 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;
}
}

The IDE is Netbeans 8.1 with Glassfish 4.0 and Jersey 2.25

  • Why you have to use Flushmodetype.COMMIT and the flush method after persist?

  • @Henrique Luiz, good morning. I’m using this because when I sent this code I had done according to a tutorial I saw that said to use this Flushmodetype to avoid the error I’m having, but as you can see it avoids nothing rs. If I just commit it doesn’t make a mistake but it doesn’t persist anything in my database, reading some posts I saw the staff saying to use this flush() method. PS: I have tried without the Flushmodetype but still the error happens. Abs.

1 answer

0


I even solved the problem, it was just a lack of attention from me in my addProduct method I was doing this: Entitymanager at = getEm(); in.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 replace the em.flush() with the em.merge() and remove the em.setFlushMode that wasn’t working for anything, so I was able to enter data without problem!

Updated addProduct method code:

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.

Browser other questions tagged

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