【问题标题】:Testing jpa entity classes - error Transaction is required测试 jpa 实体类 - 需要错误事务
【发布时间】:2015-01-20 13:27:55
【问题描述】:

基于archetype,我创建了一个 java ee 应用程序。包含一个运行良好的 arquillian 测试。它只是调用@Stateless bean 上的一个方法,该方法保留了一个预制实体。

现在我添加了一些具有某些关系的实体,并为它们编写了一个测试。但是在持久化任何实体时,我得到了

Transaction is required to perform this operation (either use a transaction or extended persistence context)

我想我需要用@Transactional 标记测试方法,但它似乎不在类路径中。 在注入的 EntityManager 上手动调用事务会产生另一个错误。 那么如何正确设置此类测试和依赖项。

编辑 正如 Grzesiek D. 所建议的,这里有一些细节。这是实体(连接其他实体的实体):

@Entity
public class Booking implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * internal id.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
    /**
     * Used for optimistic locking.
     */
    @Version
    @Column(name = "version")
    private int version;

    /**
     * A booking must have a project related.
     */
    @ManyToOne
    @JoinColumn(name = "project_id")
    @NotNull
    private Project project;

    /**
     * A booking must have an owner.
     */
    @ManyToOne
    @JoinColumn(name = "user_id")
    @NotNull
    private User owner;

    /**
     * A booking always has a start time.
     */
    @Column
    @NotNull
    private Timestamp start;

    /**
     * A booking always has an end time.
     */
    @Column
    @NotNull
    private Timestamp end;

    /**
     * 
     * @return true if start is befor end. false otherwise (if equal or after end).
     */
    @AssertTrue(message = "Start must before end.")
    public final boolean isStartBeforeEnd() {
        return start.compareTo(end) < 0;
    }

    /**
     * @return the id
     */
    public final Long getId() {
        return id;
    }

    /**
     * @param id
     *            the id to set
     */
    public final void setId(final Long id) {
        this.id = id;
    }

    /**
     * @return the version
     */
    public final int getVersion() {
        return version;
    }

    /**
     * @param version
     *            the version to set
     */
    public final void setVersion(final int version) {
        this.version = version;
    }

    /**
     * @return the project
     */
    public final Project getProject() {
        return project;
    }

    /**
     * @param project
     *            the project to set
     */
    public final void setProject(final Project project) {
        this.project = project;
    }

    /**
     * @return the owner
     */
    public final User getOwner() {
        return owner;
    }

    /**
     * @param owner
     *            the owner to set
     */
    public final void setOwner(final User owner) {
        this.owner = owner;
    }

    /**
     * @return the start
     */
    public final Timestamp getStart() {
        return start;
    }

    /**
     * @param start
     *            the start to set
     */
    public final void setStart(final Timestamp start) {
        this.start = start;
    }

    /**
     * @return the end
     */
    public final Timestamp getEnd() {
        return end;
    }

    /**
     * @param end
     *            the end to set
     */
    public final void setEnd(final Timestamp end) {
        this.end = end;
    }

    //hashCode, equals, toString omitted here
}

这是测试:

@RunWith(Arquillian.class)
public class BookingTest {

    @Deployment
    public static Archive<?> createDeployment() {
        return ArquillianContainer.addClasses(Resources.class, Booking.class, Project.class, User.class);
    }

    @Inject
    private EntityManager em;

    @Test
    public void createBooking() {
        Booking booking = new Booking();
        booking.setStart(new Timestamp(0));
        booking.setEnd(new Timestamp(2));
        User user = new User();
        user.setName("Klaus");
        booking.setOwner(user);
        Project project = new Project();
        project.setName("theOne");
        project.setDescription("blub");
        booking.setProject(project);
        em.persist(booking);
        System.out.println("here");
    }

}

这里有一个例外:

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

我知道如果我创建一个 @Stateless bean 并在那里封装持久性,它会起作用,但我想要直接测试实体的验证,我需要一个游乐场来发展数据模型。

【问题讨论】:

  • 直接调用事务方法会出现什么错误?可能注入的 EntityManager 不允许显式事务(如 spring 管理)如果是这样,您将不得不添加 Transactional 的依赖项
  • @Transactional 注释来自 Spring 世界 - 在 JEE 世界中它不存在,所以忘记它。在 JEE 中,您有 @Stateless 注释,通常应该足够了 - 因为它将每个公共方法包装在事务中。请发布整个 stacktrace 和有问题的 source code 的 sn-p - 没有它没有人可以帮助你。
  • 更新了我的问题,在我的情况下,我希望它在没有 bean 的情况下运行,所以应该是 arquillian 中的一些事务性内容(我尝试了 Arquillian 的 @Transactional,但它没有用)

标签: java jpa jboss-arquillian


【解决方案1】:

为了在 Arquillian 测试中获得事务支持,您需要引入 extension which enables this feature。在您的情况下,jta 依赖项应该可以完成这项工作。

<dependency>
  <groupId>org.jboss.arquillian.extension</groupId>
  <artifactId>arquillian-transaction-jta</artifactId>
  <scope>test</scope>
</dependency>

另外,如果你使用的是JBoss,你需要为UserTranscation提供它的JNDI,所以把下面的部分放在你的arquillian.xml中:

<?xml version="1.0" ?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/schema/arquillian" xsi:schemaLocation="http://jboss.org/schema/arquillian
    http://jboss.org/schema/arquillian/arquillian_1_0.xsd">     

  <extension qualifier="transaction">
    <property name="manager">java:jboss/UserTransaction</property>
  </extension>

</arquillian>

这样你就可以使用来自这个扩展API的@Transactional

【讨论】:

  • 它正在工作! -谢谢。我唯一需要更改的是将版本添加到依赖项(扩展似乎不包含在 bom 中)
猜你喜欢
  • 2017-07-22
  • 2016-02-13
  • 2017-09-16
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
相关资源
最近更新 更多