使用JPA / JTA / JBOSS / CDI不会持久保存对象 [英] Object is not getting persisted using JPA/JTA/JBOSS/CDI

查看:109
本文介绍了使用JPA / JTA / JBOSS / CDI不会持久保存对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我理解为什么对象没有得到以下代码的持久性。它抛出 javax.persistence.TransactionRequiredException:JBAS011469:执行此操作需要事务(使用事务或扩展持久化上下文)

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();

        }
    }    
}

但是如果我使用 @Stateless 注释(现在ejb)注释该类,对象开始获取持久性

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>

调用Repository以将对象持久保存到数据库的服务类

    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;
    }
}  


推荐答案

你将bean声明为 @Stateless ,那么该bean中的方法默认是事务性的。事务性方法在完全执行时提交持久性状态。

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.

当您没有使用 @Stateless 注释类时默认情况下,方法不是事务性的,因此您会得到上述异常。

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

这篇关于使用JPA / JTA / JBOSS / CDI不会持久保存对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆