简体   繁体   English

使用JPA / JTA / JBOSS / CDI无法持久保存对象

[英]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) 它引发javax.persistence.TransactionRequiredException:JBAS011469:需要事务才能执行此操作(使用事务或扩展的持久性上下文)

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批注(现在为ejb)对类进行批注,则对象将开始保留

@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 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. 当您将bean声明为@Stateless ,默认情况下该bean中的方法是事务性的。 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. 当您没有用@Stateless方法注释的类时,默认情况下不会进行事务处理,因此您会遇到上述异常。

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. 我对EJB不太熟悉,但是我相信问题在于,如果没有@Stateless批注,则bean不是EJB,因此不会自动管理事务。 If you need statefulness, use the @Stateful annotation to make a stateful EJB. 如果需要有状态,请使用@Stateful批注创建有状态EJB。 If you do not want to use EJBs, you have to manually manage your transactions using the EntityManager like so. 如果您不想使用EJB,则必须像这样使用EntityManager手动管理事务。

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

// do some work
...

tx.commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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