我在persistence.xml的单元测试中遇到有关JPA的问题。
在将对象持久存储到内存中的数据库H2时,发生错误:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running some.class.MyClass
################################################################################
                    BeforeClass Start
################################################################################
                    EntityManager is being created..
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.541 sec <<< FAILURE!

Results :

Tests in error:
  some.class.MyClass: getSingleResult() did not retrieve any entities


我确信持久性存在问题,而不是获取数据存在问题,因为DEV应用程序中的读取方法与真实数据库完美配合。

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
    <persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL">

        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>all entities</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
            <property name="eclipselink.logging.level" value="SEVERE"/>
            <!-- ALL SEVERE -->
            <property name="eclipselink.logging.level.sql" value="SEVERE"/>
            <!-- ALL SEVERE -->
            <property name="eclipselink.logging.parameters" value="true"/>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;MODE=Oracle;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS DEV"/>
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <!-- model creation have to be done via javax.persistence, not hibernate/eclipselink - otherwise import doesn't works -->
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
            <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
            <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
        </properties>

    </persistence-unit>
</persistence>


</persistence-unit>




这是一段Java代码:

@BeforeClass
        public static void setUpClass() {
                myOut("BeforeClass Start");
                EM = Persistence.createEntityManagerFactory(PU).createEntityManager();
                myOut("EntityManager is being created..");
                EM.persist(new Account(PU, PU, PU));
                EM.persist(new Account(PU + "2", "dsaddsa", "dsadas"));
                EM.persist(new Account(PU + "3", "saddsd", "dsadasa"));
                EM.persist(new Account(PU + "4", "adsada", "hdsd"));

                final DaoLogged dao = new DaoLogged();
                dao.setEntityManager(EM);
                dao.getAccountByLogin(PU);
                myOut("BeforeClass Done.");
        }


SingleResult方法:

public Account getAccountByLogin(String login) {
                return (Account) getEntityManager().createNamedQuery("Account.findByLogin").setParameter("login", login).getSingleResult();
}


我将不胜感激! :)
问候!

[编辑]

我一直在寻找一个答案很长的时间,实际上在今天的早晨,这完全没有用,但是经过几个小时的编辑,搜索了Internet,但我还是坚持了下来。

[编辑2]

我已将上述方法更改为:

@BeforeClass
        public static void setUpClass() {
                myOut("BeforeClass Start");
                EM = Persistence.createEntityManagerFactory(PU).createEntityManager();
                myOut("EntityManager is being created..");

                **EM.getTransaction().begin();**

                EM.persist(new Account(PU, PU, PU));
                EM.persist(new Account(PU + "2", "dsaddsa", "dsadas"));
                EM.persist(new Account(PU + "3", "saddsd", "dsadasa"));
                EM.persist(new Account(PU + "4", "adsada", "hdsd"));
                **EM.getTransaction().commit();
                EM.flush();
                EM.getTransaction().begin();**
                final DaoLogged dao = new DaoLogged();
                dao.setEntityManager(EM);
                dao.getAccountByLogin(PU);
                myOut("BeforeClass Done.");
        }


仍然是错误,但有所不同:

javax.persistence.TransactionRequiredException:


异常描述:当前没有事务处于活动状态

最佳答案

该错误表示查询未返回任何结果。确保数据库设置正确。为此,为EclipseLink启用DEBUG日志级别。那应该向您显示正在发送到数据库的SQL。

您还需要一名交易经理。如何设置和配置它取决于您的应用程序的体系结构。事务管理器负责协调事务的所有不同参与者(数据库,EntityManager,有时还包括JMS等其他各方)。

如果您使用Spring,则事务管理器将自动将事务与测试同步。

请注意,您应该尝试在每次测试之前启动事务,并在每次测试之后回滚。这样,测试#2不会失败,因为测试#1在数据库中留下了垃圾。

10-01 23:26
查看更多