views:

58

answers:

1

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("hibernate.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...

A: 

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();
Pascal Thivent
Nice point, however id didn't work :( same thing.
jmatter
@jmatter: That's weird. I tried before posting (to double check the properties were ok) and it worked for me. Do you get the same error?
Pascal Thivent
@Pascal: yes, i get the very same error. what is interesting perhaps is that i get a bunch of warnings when hibernate parses the persistence.xml file because for a peculiar reason it cannot find the xsd. but i really don't think it has to do much with it...
jmatter
@jmatter: I can at least confirm that you need to prefix the properties by `hibernate.` so the above is correct. Can you show the "header" or your persistence.xml?
Pascal Thivent