简体   繁体   中英

Object is not getting persisted using JPA/JTA/JBOSS/CDI

Please help me to understand why object is not getting persisted with following code. It throws javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context)

public class OrganizationRepositoryImpl implements OrganizationRepository {

    @PersistenceContext(unitName="usermanagement",type=PersistenceContextType.TRANSACTION)
    private EntityManager em;

    public void save(Organization organization) {
        try {
            em.persist(organization);
        }catch(Exception e) {
            e.printStackTrace();

        }
    }    
}

But If I annotate the class with @Stateless annotation(Now ejb), object start getting persisted

@Stateless    
public class OrganizationRepositoryImpl implements OrganizationRepository {

            @PersistenceContext(unitName="usermanagement",type=PersistenceContextType.TRANSACTION)
            private EntityManager em;

            public void save(Organization organization) {
                try {
                    em.persist(organization);
                }catch(Exception e) {
                    e.printStackTrace();

                }
            }    
        }

persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
        version="2.1">
    <persistence-unit name="usermanagement" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/MysqlXADS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
             <property name="hibernate.show_sql" value="true"/>
             <property name="hibernate.format_sql" value="true"/>  
             <property name="use_sql_comments" value="true"/>
             <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/myEntityManagerFactory" /> 
        </properties>
    </persistence-unit>
</persistence>

Service class to call Repository to persist the object to the database

    public class OrganizationServiceImpl implements OrganizationService {

    @Inject
    private OrganizationRepository orgRepo;

    public Response createOrganization(InputStream is) {
        Organization org = null;
        org = readStream(is);
        orgRepo.save(org);
        return Response.created(URI.create("/organizations/" + org.getId()))
                .build();
    }

    private Organization readStream(InputStream is) {
        JAXBContext context;
        Organization org = null;
        try {
            context = JAXBContext.newInstance(Organization.class);
            Unmarshaller um = context.createUnmarshaller();
            org = (Organization) um.unmarshal(is);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return org;
    }
}  

When you declare the bean as @Stateless then the methods in that bean are by default transactional. Transactional methods commit the persistence state when fully executed.

When you don't have your class annotated with @Stateless methods are not transactional by default and hence you get the mentioned exception.

I'm not too familiar with EJBs, but I believe the problem is that without the @Stateless annotation, the bean is not a EJB, so transactions are not automatically managed. If you need statefulness, use the @Stateful annotation to make a stateful EJB. If you do not want to use EJBs, you have to manually manage your transactions using the EntityManager like so.

tx = em.getTransaction();
tx.begin();

// do some work
...

tx.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM