2015-11-04 51 views
0

我收到奇怪的問題。我正在嘗試進行集成測試,但實體管理器創建器無法正常工作。創建EntityManager + Junit + HSQLDB的問題

<? xml version = "1.0" encoding = "UTF-8"?> 
<persistence 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" 
    version = "2.0"> 
  
    <persistence-unit name = "db-test" transaction-type = "RESOURCE_LOCAL"> 
     
        <provider> org.hibernate.jpa.HibernatePersistenceProvider </ provider> 

        <class> br.com.e2pd.model.Grupo </ class> 
  
        <properties> 
        <property name = "javax.persistence.jdbc.url" value = "jdbc: hsqldb: mem: db-test" /> 
<property name = "javax.persistence.jdbc.user" value = "sa" /> 
<property name = "javax.persistence.jdbc.password" value = "" /> 
<property name = "javax.persistence.schema-generation.database.action" value = "drop-and-create" /> 
<property name = "hibernate.show_sql" value = "true" /> 
            <property name = "hibernate.hbm2ddl.auto" value = "update" /> 
        </ properties> 
    </ persistence-unit> 
</ persistence> 

public class RepositoryTest { 

    protected static EntityManagerFactory factory; 
    protected static EntityManager em; 

    @BeforeClass 
    public static void setUpPersistence() { 
     factory = Persistence.createEntityManagerFactory("db-test"); 
     em = factory.createEntityManager(); 
     em.getTransaction().begin(); 
    } 

    @AfterClass 
    public static void tearDown() { 
     em.getTransaction().commit(); 
     em.close(); 
     factory.close(); 
    } 
} 

public class GrupoRepositoryTest extends RepositoryTest { 

    private GrupoRepository repository; 

    @Before 
    public void setUp() { 

     this.repository = new GrupoRepositoryImpl(em); 
    } 
} 


@Entity 
@Table(name = "grupos") 
public class Grupo implements Model{ 

    @Id 
    @GeneratedValue 
    @Column(name = "id") 
    private Long id; 

    @Column(name = "nome") 
    private String nome; 

    public Grupo() { 
    } 

    public Grupo(Long id, String nome) { 
     this.id = id; 
     this.nome = nome; 
    } 

    //gets and sets 
} 

的日誌出現在這裏:

20: 53: 28.484 INFO [LogHelper] HHH000204: Processing PersistenceUnitInfo [ 
name: DB-test 
...] 
20: 53: 28.582 INFO [Version] HHH000412: Hibernate Core 5.0.2.Final {} 
20: 53: 28.584 INFO [Environment] HHH000206: hibernate.properties not found 
20: 53: 28.586 INFO [Environment] HHH000021: Bytecode provider name: Javassist 
20: 53: 39.198 INFO [Version] HCANN000001: Hibernate Commons Annotations 5.0.0.Final {} 
20: 55: 45.714 WARN [DriverManagerConnectionProviderImpl] HHH000402: Using Hibernate built-in connection pool (not for production use!) 
20: 55: 45.732 INFO [DriverManagerConnectionProviderImpl] HHH000401: using driver [null] at URL [jdbc: hsqldb: mem: db-test] 
20: 55: 45.733 INFO [DriverManagerConnectionProviderImpl] HHH000046: Connection properties: user = {s} 
20: 55: 45.733 INFO [DriverManagerConnectionProviderImpl] HHH000006: Autocommit mode: false 
20: 55: 45.740 INFO [DriverManagerConnectionProviderImpl] HHH000115: Hibernate connection pool size: 20 (min = 1) 
STOPS HERE! 

,並給出了異常Caused by: java.sql.SQLException: No suitable driver found for jdbc: hsqldb: mem: db-test

爲了解決這個例外,我做到了在setUpPersistence()(甚至不知道它是否是最好的解決方案):

try { 
    Class.forName ("org.hsqldb.jdbcDriver"); 
    connection = DriverManager.getConnection ("jdbc: hsqldb: mem: db-test", "sa", ""); 
} Catch (Exception ex) { 
    ex.printStackTrace(); 
} 

其後,當道路試驗給出了錯誤:

java.lang.NoSuchMethodError: javax.persistence.Table.indexes() [Ljavax/persistence/Index; 
at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1080) 
at org.hibernate.cfg.AnnotationBinder.bindClass (AnnotationBinder.java:765) 
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245) 
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222) 
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:770) 
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:797) 
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58) 
at javax.persistence.Persistence.createEntityManagerFactory (Persistence.java:63) 
at javax.persistence.Persistence.createEntityManagerFactory (Persistence.java:47) 
at unit.br.com.e2pd.common.RepositoryTest.setUpPersistence (RepositoryTest.java:36) 
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source) 
at java.lang.reflect.Method.invoke (Unknown Source) 
org.junit.runners.model.FrameworkMethod at $ 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.RunBefores.evaluate (RunBefores.java:24) 
at org.junit.internal.runners.statements.RunAfters.evaluate (RunAfters.java:27) 
at org.junit.runners.ParentRunner.run (ParentRunner.java:363) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUnit4TestReference.java:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:192) 

java.lang.NullPointerException 
at unit.br.com.e2pd.common.RepositoryTest.tearDown (RepositoryTest.java:43) 
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source) 
at java.lang.reflect.Method.invoke (Unknown Source) 
org.junit.runners.model.FrameworkMethod at $ 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.RunAfters.evaluate (RunAfters.java:33) 
at org.junit.runners.ParentRunner.run (ParentRunner.java:363) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUnit4TestReference.java:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:192) 

回答