2011-10-12 90 views
0

打開JPA EntityManager的事務我使用Spring 3.0,JPA 2.0 MyEclipse的IDE無法在彈簧試驗

嘗試一些基本的DAO集成測試轉換成春,遇到了一些問題的Java項目。這裏是我的設置:

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> 

起初我收到這個

值 '2.0' 元素 '持久性' 的屬性 '版本' 是無效

所以閱讀另一篇建議將其改爲「1.0」的帖子

現在收到

java.lang.IllegalStateException:無法加載的ApplicationContext

這個問題是與<提供商>標籤但是沒有固定我的問題解決了:

org.springframework.transaction .CannotCreateTransactionException:無法打開JPA EntityManager進行事務處理;嵌套異常是異常[EclipseLink-4021](Eclipse持久性服務 - 1.0.2(內部版本20081024)):org.eclipse.persistence.exceptions.DatabaseException 異常說明:無法從驅動程序[null]獲取連接,用戶[null ]和URL [null]。

我的問題是,通過降級版本號。到「1.0」,我也忽略了這個堆棧中的兼容性問題。我有這些測試的非春季版本的工作,所以任何想法,我出錯的地方讚賞。

由於

回答

3
  • 無法獲取來自驅動器[空],用戶[空]和URL [空]

連接這些是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

這裏是JPA 2.0以上的EclipseLink是工作的例子(注意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> 

看看在example in full