无法在 Spring Test 中打开 JPA EntityManager 进行事务处理

发布于 2024-12-09 12:07:05 字数 3400 浏览 0 评论 0原文

我有一个使用 Spring 3.0 、 JPA 2.0 和 MyEclipse IDE 的 java 项目,

试图将一些基本的 dao 集成测试转换为 spring,但遇到了一些问题。这是我的设置:

LevelDAO

public class LevelDAO extends JpaDaoSupport implements ILevelDAO 
{
public void save(Level entity) {
    logger.info("saving Level instance");
    try {
        getJpaTemplate().persist(entity);
        logger.info("save successful");
    } catch (RuntimeException re) {
        logger.error("save failed", re);
        throw re;
    }
}

}

单元测试

@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
public class LevelDaoImplTests {

 LevelDAO levelDao;

 @Test
 public void shouldSaveNewLevels() {

        levelDao= new LevelDAO();
        Level l = new Level();   
        l.setName = "test";
        levelDao.save(l); 

    }

}

Persistence.Xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="t1" transaction-type="RESOURCE_LOCAL">
    <provider>
        org.eclipse.persistence.jpa.PersistenceProvider
    </provider>
    <class>com.nlg.model.Level</class>
    <properties>
        <property name="javax.persistence.jdbc.driver"
            value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url"
            value="jdbc:mysql://myserver.rds.amazonaws.com:3306/" />
        <property name="javax.persistence.jdbc.user" value="myuser" />
        <property name="javax.persistence.jdbc.password" value="mypass" />
    </properties>
</persistence-unit>

applicationContext.xml

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="t1" />
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean
    id="LevelDAO" class="com.nlg.model.LevelDAO">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>

首先,我收到了

元素“persistence”的属性“version”的值“2.0”无效

因此,阅读另一篇文章,建议将其更改为“1.0” java.lang.IllegalStateException

现在收到

: 无法加载 ApplicationContext

该问题已通过 < 解决供应商>但是标签还没有解决我的问题:

org.springframework.transaction.CannotCreateTransactionException:无法打开JPA EntityManager进行事务;嵌套异常是 Exception [EclipseLink-4021] (Eclipse Persistence Services - 1.0.2 (Build 20081024)): org.eclipse.persistence.exceptions.DatabaseException 异常描述:无法从驱动程序 [null]、用户 [null] 和 URL [null] 获取连接。

我担心的是通过降级版本号。到“1.0”,我也忽略了这个堆栈内的兼容性问题。我有这些测试的非春季版本,因此任何关于我出错的地方的想法都值得赞赏。

谢谢

I have a java project using Spring 3.0 , JPA 2.0 with MyEclipse IDE

Trying to convert some basic dao integration tests to spring and have run into a few issues. Here's my setup:

LevelDAO

public class LevelDAO extends JpaDaoSupport implements ILevelDAO 
{
public void save(Level entity) {
    logger.info("saving Level instance");
    try {
        getJpaTemplate().persist(entity);
        logger.info("save successful");
    } catch (RuntimeException re) {
        logger.error("save failed", re);
        throw re;
    }
}

}

Unit Test

@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
public class LevelDaoImplTests {

 LevelDAO levelDao;

 @Test
 public void shouldSaveNewLevels() {

        levelDao= new LevelDAO();
        Level l = new Level();   
        l.setName = "test";
        levelDao.save(l); 

    }

}

Persistence.Xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="t1" transaction-type="RESOURCE_LOCAL">
    <provider>
        org.eclipse.persistence.jpa.PersistenceProvider
    </provider>
    <class>com.nlg.model.Level</class>
    <properties>
        <property name="javax.persistence.jdbc.driver"
            value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url"
            value="jdbc:mysql://myserver.rds.amazonaws.com:3306/" />
        <property name="javax.persistence.jdbc.user" value="myuser" />
        <property name="javax.persistence.jdbc.password" value="mypass" />
    </properties>
</persistence-unit>

applicationContext.xml

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="t1" />
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean
    id="LevelDAO" class="com.nlg.model.LevelDAO">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>

At first I recieved this

Value '2.0' of attribute 'version' of element 'persistence' is not valid

So reading another post which suggested changing this to "1.0"

and now recieve

java.lang.IllegalStateException: Failed to load ApplicationContext

That issue was resolved with the < provider > tag however hasn't fixed my issue:

org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is Exception [EclipseLink-4021] (Eclipse Persistence Services - 1.0.2 (Build 20081024)): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].

My concern is that by downgrading the version no. to "1.0" that I'm also overlooking the issue of compatibility within this stack. I have non-spring versions of these tests working, so any ideas on where I've gone wrong appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-12-16 12:07:06
  • 无法从驱动程序 [null]、用户 [null] 和 URL [null] 获取连接

这些是 JPA 2.0 属性:

 <property name="javax.persistence.jdbc.driver"
        value="com.mysql.jdbc.Driver" />
 <property name="javax.persistence.jdbc.url"
        value="jdbc:mysql://myserver.rds.amazonaws.com:3306/" />
 <property name="javax.persistence.jdbc.user" value="myuser" />
 <property name="javax.persistence.jdbc.password" value="mypass" />

因此,如果您切换回 1.0,您可能会想要:

<property name="eclipelink.jdbc.url" value="jdbc:mysql://myserver.rds.amazonaws.com:3306/"/> 
<property name="eclipelink.jdbc.user" value="myuser"/> 
<property name="eclipelink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
<property name="eclipelink.jdbc.password" value="mypass"/>

但是然后我建议解决“其他问题”并继续使用 2.0

下面是一个通过 EclipseLink 运行的 JPA 2.0 示例(注意 persistence version="2.0" ):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="eclipselinktest" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <!-- list all classes -->
    <class>com.codesmuggler.model.User</class>

    <properties>
      <!-- some properties needed by persistence provider:
        - driver
        - db url
        - db user name
        - db user password -->
      <property name="javax.persistence.target-database" value="PostgreSQL"/>
      <property name="javax.persistence.logging.level" value="INFO"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb"/>
      <property name="javax.persistence.jdbc.user" value="testuser"/>
      <property name="javax.persistence.jdbc.password" value="testpassword"/>

      <!-- for testing purpose every time application is launched drop and create tables
        in production mode - this line should be removed or commented out
      -->
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />

    </properties>

  </persistence-unit>     
</persistence>

看一下 完整示例

  • Unable to acquire a connection from driver [null], user [null] and URL [null]

These are JPA 2.0 properties:

 <property name="javax.persistence.jdbc.driver"
        value="com.mysql.jdbc.Driver" />
 <property name="javax.persistence.jdbc.url"
        value="jdbc:mysql://myserver.rds.amazonaws.com:3306/" />
 <property name="javax.persistence.jdbc.user" value="myuser" />
 <property name="javax.persistence.jdbc.password" value="mypass" />

So if you switched back to 1.0, you'd probably want:

<property name="eclipelink.jdbc.url" value="jdbc:mysql://myserver.rds.amazonaws.com:3306/"/> 
<property name="eclipelink.jdbc.user" value="myuser"/> 
<property name="eclipelink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
<property name="eclipelink.jdbc.password" value="mypass"/>

But then I would recommend to solve "that other problem" and stay with 2.0

Here is an example of JPA 2.0 over EclipseLink that works ( notice persistence version="2.0" ):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="eclipselinktest" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <!-- list all classes -->
    <class>com.codesmuggler.model.User</class>

    <properties>
      <!-- some properties needed by persistence provider:
        - driver
        - db url
        - db user name
        - db user password -->
      <property name="javax.persistence.target-database" value="PostgreSQL"/>
      <property name="javax.persistence.logging.level" value="INFO"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb"/>
      <property name="javax.persistence.jdbc.user" value="testuser"/>
      <property name="javax.persistence.jdbc.password" value="testpassword"/>

      <!-- for testing purpose every time application is launched drop and create tables
        in production mode - this line should be removed or commented out
      -->
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />

    </properties>

  </persistence-unit>     
</persistence>

Take a look at the example in full

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文