使用JPA / JTA / JBOSS / CDI不会持久保存对象

时间:2014-04-25 18:03:11

标签: java java-ee jpa jboss7.x jta

请帮助我理解为什么对象没有得到以下代码的持久性。它抛出 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();

        }
    }    
}

但如果我使用 @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

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

2 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:4)

我对EJB并不太熟悉,但我认为问题在于没有@Stateless注释,bean不是EJB,因此不会自动管理事务。如果需要有状态,请使用@Stateful注释创建有状态EJB。如果您不想使用EJB,则必须使用EntityManager手动管理事务。

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

// do some work
...

tx.commit();