2016-10-12 150 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