Unable to initialize EntityManager in Netbeans via Junit

I have a class of services (ejb 3) that I want to unit test. To do this, I added an overloaded constructor to my implementation classes, which takes an EntityManager as an argument. The idea is that during my unit tests I will create an instance of EntityManager from a storage unit specific to my unit tests and an instance of my class of service using an overloaded constructor. There, I pass an EntityManger created from mine as part of 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 here 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)

, , :

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

, , persistence.xml , .

netbeans 6.9.

! ...

+3
3

:

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

, , "Test Librearies" ( jdbc, ,..)

0
source

Source: https://habr.com/ru/post/1771308/


All Articles