【问题标题】:javax.persistence.TransactionRequiredException Jboss eapjavax.persistence.TransactionRequiredException Jboss eap
【发布时间】:2016-10-12 16:45:42
【问题描述】:

我有一个如下所示的实体类:

@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)

为了尝试解决这个问题,我正在做一些事情来验证什么有效,什么无效:

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

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

  • 我已将 , type = 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>
    

但是,我仍然无法摆脱这个问题。我在这里遗漏了什么明显的东西?

【问题讨论】:

    标签: hibernate jpa jboss


    【解决方案1】:

    如下使用@Transactional

    @Transactional
    public class MyStarter {
    ...
    

    或作为

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

    这是因为,persist() 保证如果在事务边界之外调用INSERT/UPDATE 语句,它将执行它。
    因此,您需要一个事务来将对象保存在 db 中,同时使用 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 />
    

    您可以使用save() 代替persist()save() 方法可以在事务内部或外部执行。使用 save() 在具有扩展会话/持久性上下文的长期对话中好。

    【讨论】:

    • 什么是事务边界?在实体类之外?
    • 你的意思是@Transactional在课堂上申请?我是这样,然后@Transactional 应用于您的 MyStarter 类的每个单独的方法。并调用 insertIntodb() -> 开始事务。当 insertIntodb() 调用另一个方法时,不会启动新事务,因为已经有一个。请记住,您不能在外部事务中使用persist()。它不会被执行。
    • 啊,我明白了。最奇怪的是,我看到一些项目没有明确使用该注解,并调用 .persist() 方法,没有任何问题。
    • 是的,可以有这样的项目,但在这些项目中完成的事务管理将是 declarative 使用 &lt;tx:advice /&gt; 标签和 AOP 配置。这是一个很好的做法。
    猜你喜欢
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多