开发者

Cannot initialize EntityManager in Netbeans via Junit

开发者 https://www.devze.com 2023-01-21 02:17 出处:网络
I have a bunch of service (ejb 3) classes that i want to unit test. In order to do so, i have added to their implementing classes an overloaded constructor which takes an EntityManager as an argument.

I have a bunch of service (ejb 3) classes that i want to unit test. In order to do so, i have added to their implementing classes an overloaded constructor which takes an EntityManager as an argument. The idea is that during my units tests i will create a both an EntityManager instance from a persistence unit specific for my unit tests, and an instance of my service class using the overloaded constructor. There, i pass the EntityManger created from my within my unit tests.

My PU is configured as follows:

<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>

And this is what i do to create my EntityManager in my unit test:

    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("hibernat开发者_运维技巧e.show_sql", "true");
    emf = Persistence.createEntityManagerFactory("MyPU-junit", config);
    em = emf.createEntityManager();

The problem is that i get this error:

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)

I'm not sure it's related with the previous error but I'm also getting a bunch of warnings from the parser during initialization:

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

Note that i did tried to run the same thing by putting the hibernate parameters in the persistence.xml file directly instead of programmatically.

I am using netbeans 6.9.

Any help is REALLY welcomed! I'm truly frustrated with this...


Use the following:

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();


Regarding the parser warnings, maybe your xml is wrong? Try to enclose your configuration with the following (which is extracted from a valid 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>


I fixed a similar problem making sure that all the necesary libraries are in the "Test Librearies" folder ( jdbc driver, hibernate libraries, .. )

0

精彩评论

暂无评论...
验证码 换一张
取 消