无法在 Spring Test 中打开 JPA EntityManager 进行事务处理
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些是 JPA 2.0 属性:
因此,如果您切换回 1.0,您可能会想要:
但是然后我建议解决“其他问题”并继续使用 2.0
下面是一个通过 EclipseLink 运行的 JPA 2.0 示例(注意
persistence version="2.0"
):看一下 完整示例
These are JPA 2.0 properties:
So if you switched back to 1.0, you'd probably want:
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"
):Take a look at the example in full