2017-09-19 2 views
0

私は複数のdb接続が必要なアプリケーションで作業していました。私は、特定のEntityクラスに定義されたEntityManagerを使用したいAbstractDao.classを作成しました。私の例では、OracleEmployeeEntityとMySQLEmployeeEntityの2種類のエンティティを定義しました。正しいエンティティに正しいインスタンスを使用するためにAbstractDaoでEntityManagerを定義する方法は?私の現在の設定の下で、使用法を使ってテストを実行していたときに、2つのdbs NullPointerExceptionがスローされました。JPA特定のエンティティクラスにどのパーシスタンスユニットを使用するかを決める方法

AbstractDao.class:

@Transactional 
public abstract class AbstractDao<T, K extends Serializable> implements IDao<T, K> { 

@PersistenceContext 
protected EntityManager entityManager; 

private Class<T> domainClass; 

@Override 
public T save(T entity) { 
    entityManager.persist(entity); 
    return entity; 
} 

@Override 
@SuppressWarnings("unchecked") 
public T getOne(K id) { 
    return entityManager.getReference(getDomainClass(), id); 
} 

[...] 

persistence.xmlファイル:テストの

<persistence 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" 
     version="2.1"> 

<persistence-unit name="default" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <class>com.example.database.tests.entities.OracleEmployeeEntity</class> 
    <properties> 
     <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/> 
     <property name="javax.persistence.jdbc.user" value="test_user"/> 
     <property name="javax.persistence.jdbc.password" value="test_user"/> 
     <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521/xe"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/> 
     <property name="hibernate.flushMode" value="FLUSH_AUTO"/> 
     <property name="hibernate.show_sql" value="true"/> 
    </properties> 
</persistence-unit> 

<persistence-unit name="db1" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <class>com.example.database.tests.entities.OracleEmployeeEntity</class> 
    <properties> 
     <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/> 
     <property name="javax.persistence.jdbc.user" value="test_user"/> 
     <property name="javax.persistence.jdbc.password" value="test_user"/> 
     <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521/xe"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/> 
     <property name="hibernate.flushMode" value="FLUSH_AUTO"/> 
     <property name="hibernate.show_sql" value="true"/> 
    </properties> 
</persistence-unit> 

<persistence-unit name="db2" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
    <class>com.example.database.tests.entities.MySqlEmployeeEntity</class> 
    <properties> 
     <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
     <property name="javax.persistence.jdbc.user" value="root"/> 
     <property name="javax.persistence.jdbc.password" value="root"/> 
     <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/employees"/> 
     <property name="hibernate.flushMode" value="FLUSH_AUTO"/> 
     <property name="hibernate.show_sql" value="true"/> 
    </properties> 
</persistence-unit> 

パート:

//db variable defined in @Before (jUnit) 
    @Test 
    public void testShouldCompareDataFromDifferentDatabases() { 
    //given 
    MySQLDatabase mySQLDatabase = new MySQLDatabase(); 

    //when 
    long oracleCount = db.getEmployeeDao().count(); 
    long mySqlCount = mySQLDatabase.getEmployeeDao().count(); 

    //then 
    assertNotEquals(oracleCount, mySqlCount); 
} 

スタックトレース:

java.lang.NullPointerException 
at com.example.database.base.AbstractDao.count(AbstractDao.java:74) 
at com.example.database.tests.assertions.JpaTest.testShouldCompareDataFromDifferentDatabases(JpaTest.java:166) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at com.example.core.tests.testRunners.core.CustomRunAfters.evaluate(CustomRunAfters.java:39) 
at com.example.core.tests.testRunners.ParallelTestClassRunner.run(ParallelTestClassRunner.java:36) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
+0

詳細な例外をしてください使用したいのunitNameを指定する必要が空にすることがデフォルトユニット名を指定していないことから、unitName属性を持っています。どのサーバーを使用しますか?どのようなアプリケーション/環境ですか? –

+0

stacktraceがテストの一部で更新されました – pwoj

答えて

0

@PersistenceContextあなたはstring.youあなたが

@PersistenceContext(unitName= "someunitname") 
protected EntityManager entityManager; 
+0

残念ながら、まだ同じ例外があります。 @PersistenceContextはEntityManagerのインスタンスを作成しませんが、これは問題ですか?それ以外は、AbstractDaoは一般的なクラスです。永続性ユニットのハードコーディング私は具体的な実装に移りたいと思います。 – pwoj

+0

@pwoj例外スタックトレースはどこですか?これは基本的なものです –

関連する問題