2016-10-12 156 views
0

我有如下所示的实体类:javax.persistence.TransactionRequiredException的Jboss EAP

@Entity 
@Table(name = "MY_TABLE") 
public class MyEntity implements Serializable { 

    @Id 
    @Column(name = "ID") 
    @XmlAttribute 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private String id; 

    @Column(name = "NAME") 
    @XmlAttribute 
    private String name; 

    public String getId() { 
     return id; 
    } 

    public void setId(final String id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(final String name) { 
     this.name= name; 
    } 
} 

这个实体是由该类坚持:

public class MyStarter { 

    @PersistenceContext(unitName = "myUnit") 
    private EntityManager manager; 

    public void insertIntodb() { 
     final MyEntity entity = new MyEntity(); 
     entity.setName("Sample"); 
     manager.persist(entity); 
    } 
} 

然而,每当坚持代码获取执行上面,我得到一个例外:

javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context) 

要尝试解决这个问题,这里是我做一个BC ktrack的东西,以验证什么可行,什么不是:

  • 我已验证用户连接到数据库具有写入权限。

  • 我已验证选择数据的本机查询确实可以从JPA执行并返回结果。

  • 我已经加入,类型= javax.persistence.PersistenceContextType.EXTENDED)到@PersistanceContext注释

  • 我已经更新了persistance.xml文件以包含这些行:

    <jta-data-source>java:jboss/datasources/Mysource</jta-data-source> 
    
    <properties> 
        <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" /> 
        <property name="hibernate.max_fetch_depth" value="3" /> 
        <property name="hibernate.hbm2ddl.auto" value="validate" /> 
        <property name="hibernate.show_sql" value="true" /> 
        <property name="hibernate.format_sql" value="true" /> 
        <property name="hibernate.id.new_generator_mappings" value="true" /> 
        <property name="hibernate.connection.isolation" value="2" /> 
        <property name="hibernate.default_schema" value="dbo" /> 
        <property name="hibernate.transaction.flush_before_completion" 
         value="true" /> 
        <property name="hibernate.transaction.jta.platform" 
         value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" /> 
    </properties> 
    

但是,我仍然无法摆脱这个问题。我在这里错过的任何明显的东西?

回答

1

使用@Transactional如下

@Transactional 
public class MyStarter { 
... 

OR为

@Transactional 
public void insertIntodb() { 
    final MyEntity entity = new MyEntity(); 
    entity.setName("Sample"); 
    manager.persist(entity); 
} 

这是因为,persist()保证它会不会如果它被称为事务边界以外执行INSERT/UPDATE声明。
因此,在使用persist()时,您需要事务来将对象保存在数据库中。

在Spring上下文文件中,你需要添加下面的代码:

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd"> 

<tx:annotation-driven /> 

相反的persist()可以使用save()。方法save()可以在事务内部或外部执行。使用save()而不是擅长与扩展会话/持久性上下文进行长时间运行的对话。

+0

什么被视为交易界限?实体类之外? – angryip

+0

你的意思是'@ Transactional'应用于课堂上吗?我如此,那么'@ Transactional'应用于MyStarter类的每个单独的方法。并且调用insertIntodb() - >一个事务被启动。当insertIntodb()调用另一个方法时,不会启动新的事务,因为已经有一个。记住,你不能使用'persist()'外部事务。它不会被执行。 –

+0

啊我明白了。最奇怪的部分是,我看到一些没有明确使用该注释的项目,并调用.persist()方法,没有问题。 – angryip