无法通过Junit初始化Netbeans中的EntityManager

时间:2010-10-25 13:25:10

标签: jpa netbeans junit

我有一堆服务(ejb 3)类,我想进行单元测试。为了做到这一点,我已经在他们的实现类中添加了一个重载的构造函数,它将EntityManager作为参数。我的想法是,在我的单元测试期间,我将从特定于我的单元测试的持久性单元创建一个EntityManager实例,并使用重载的构造函数创建一个服务类的实例。在那里,我通过我的单元测试中创建的EntityManger。

我的PU配置如下:

<persistence-unit name="MyPU-junit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source/>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>

这就是我在单元测试中创建EntityManager的方法:

    Map<String, String> config = new HashMap<String, String>();
    config.put("connection.url", "jdbc:postgresql://localhost:5432/MyDb");
    config.put("connection.username", "postgres");
    config.put("connection.password", "admin12345_");
    config.put("connection.driver_class", "org.postgresql.Driver");
    config.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
    config.put("hibernate.show_sql", "true");
    emf = Persistence.createEntityManagerFactory("MyPU-junit", config);
    em = emf.createEntityManager();

问题是我收到了这个错误:

SEVERE: could not complete schema update
java.lang.UnsupportedOperationException: The user must supply a JDBC connection
        at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:30)
        at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:27)
        at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:127)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)

我不确定它是否与之前的错误有关,但我在初始化过程中也从解析器中收到了一堆警告:

Failed to read schema document 'http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd'

请注意,我确实尝试通过直接将hibernate参数放在persistence.xml文件中而不是以编程方式运行相同的东西。

我正在使用netbeans 6.9。

真的欢迎任何帮助!我真的很沮丧...

3 个答案:

答案 0 :(得分:0)

使用以下内容:

Map<String, String> config = new HashMap<String, String>();
config.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/MyDb");
config.put("hibernate.connection.username", "postgres");
config.put("hibernate.connection.password", "admin12345_");
config.put("hibernate.connection.driver_class", "org.postgresql.Driver");
config.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
config.put("hibernate.show_sql", "true");
emf = Persistence.createEntityManagerFactory("MyPU-junit", config);
em = emf.createEntityManager();

答案 1 :(得分:0)

关于解析器警告,也许你的xml错了? 尝试使用以下内容(从有效的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>

答案 2 :(得分:0)

我修复了一个类似的问题,确保所有必要的库都在#34;测试库#34中;文件夹(jdbc驱动程序,hibernate库,..)