Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Spring transactions or EJB CMT instead

I tried to configure Spring Boot with Hibernate:

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.datalis.plugin.database.dao.TerminalsService;
import org.datalis.plugin.database.models.TerminalsModel;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Qualifier("terminalsService")
public class TerminalsDaoHibernate implements TerminalsService {

    @Autowired
    private EntityManager entityManager;

    @Override
    @Transactional
    public TerminalsModel getTerminalToken(String terminalToken) throws Exception {
        TerminalsModel terminal = null;
        Session session = entityManager.unwrap(Session.class);
        try {

            entityManager.getTransaction().begin();
            terminal = (TerminalsModel) session.get(TerminalsModel.class, terminalToken);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
            throw new Exception("Error");
        }
        return terminal;
    }
}

But I get this error:

14:47:34,323 ERROR [stderr] (default task-1) java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
14:47:34,323 ERROR [stderr] (default task-1)    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:255)

What is the proper way to configure @Transactional properly? Do I need to use Transaction is a different way?

like image 952
Peter Penzov Avatar asked Jul 12 '26 03:07

Peter Penzov


1 Answers

You are using @Transactional and still are trying to manually start a transaction. Either do manual transaction management (i.e remove the @Transactional) or embrace @Transactional by removing the manual transaction management code.

@Override
@Transactional
public TerminalsModel getTerminalToken(String terminalToken) throws Exception {
    TerminalsModel terminal = null;
    Session session = entityManager.unwrap(Session.class);
    return (TerminalsModel) session.get(TerminalsModel.class, terminalToken);
}

However I don't see why you would want to use plain Hibernate over JPA here. The same result can be achieved by using JPA.

@Override
@Transactional
public TerminalsModel getTerminalToken(String terminalToken) throws Exception {
    return entityManager.find(TerminalsModel.class, terminalToken);
}

Generally there is no need to use the plain Hibernate API over JPA with the current state of the JPA API.

like image 188
M. Deinum Avatar answered Jul 13 '26 17:07

M. Deinum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!