Unable to build entity manager factory in jar created with maven

1

Good morning,

In a project we are using Hibernate, Log4J2, Gson and some other things you can see in pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>br.org.minhaempresa</groupId>
<artifactId>meuprojeto</artifactId>
<name>meuprojeto</name>

<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>

<organization>
    <!-- Used as the 'Vendor' for JNLP generation -->
    <name>***</name>
</organization>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>

    <finalName>meuprojeto</finalName>

    <plugins>

        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.1.5</version>
            <configuration>

                            <mainClass>br.org.minhaempresa.meuprojeto.MainApp</mainClass>

                <!-- only required if signing the jar file -->
                <keyStoreAlias>example-user</keyStoreAlias>
                <keyStorePassword>example-password</keyStorePassword>
                <allPermissions>true</allPermissions>

            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.5.3</version>
            <configuration>
                <useReleaseProfile>false</useReleaseProfile>
                <checkModificationExcludes>
                    <checkModificationExclude>pom.xml</checkModificationExclude>
                </checkModificationExcludes>
                <tagNameFormat>version-@{project.version}</tagNameFormat>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>br.org.minhaempresa.meuprojeto.MainApp</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>${project.build.finalName}-${project.version}</finalName>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <!-- bind to the packaging phase -->
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>


<dependencies>

    <!-- Log4j -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>

    <!-- Impinj OctaneSDK -->
    <dependency>
        <groupId>com.impinj</groupId>
        <artifactId>octanesdk</artifactId>
        <version>1.22.0.30</version>
    </dependency>

    <!-- Pi4j dependency -->
    <dependency>
        <groupId>com.pi4j</groupId>
        <artifactId>pi4j-core</artifactId>
        <version>1.1</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>5.0.7.Final</version>
    </dependency>

    <!-- MySQL Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

    <!-- UaiCriteria -->
    <dependency>
        <groupId>uaihebert.com</groupId>
        <artifactId>uaiCriteria</artifactId>
        <version>4.0.0</version>
    </dependency>

    <!-- Gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
    </dependency>

    <!-- PDFBox -->
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>1.8.11</version>
    </dependency>

    <!-- Apache Commons lang -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>

</dependencies>

When running the program through the Run as Java Application, everything works properly, connecting to my database. Then I generate the jar with all dependencies (meuprograma-0.0.1-SNAPSHOT-jar-with-dependencies). When I run the application with java -jar meuprogram-0.0.1-SNAPSHOT-jar-with-dependencies.jar the application does not connect to the database and returns me the following message:

Unable to build entity manager factory

I opened the jar to see if persistence.xml is inside. It is! Within the project it is in the src / main / resources / META-INF folder.

My persistence.xml. Properties are in connection class:

<?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="crp" transaction-type="RESOURCE_LOCAL">
    <!-- provedor/implementacao do JPA -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>

public class Conexao {

private static final Logger logger = LogManager.getLogger("MainApp");
private static EntityManagerFactory emf = null;

public Conexao() throws Exception{

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");

    props.put("hibernate.show_sql", "false");
    props.put("hibernate.format_sql", "false");

    props.put("hibernate.hbm2ddl.auto", "update");
    props.put("hibernate.c3p0.acquire_increment", 2);
    props.put("hibernate.c3p0.min_size", 2);
    props.put("hibernate.c3p0.max_size", 10);
    props.put("hibernate.c3p0.max_statements", 50);
    props.put("hibernate.c3p0.timeout", 0);
    props.put("hibernate.c3p0.idle_test_period", 3000);
    props.put("hibernate.c3p0.checkoutTimeout", 15000);
    props.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");
    props.put("javax.persistence.jdbc.url", "jdbc:mysql://localhost/db_crp");
    props.put("javax.persistence.jdbc.user", "user");
    props.put("javax.persistence.jdbc.password", "pass");

    emf = Persistence.createEntityManagerFactory("crp", props);

}

public EntityManager getEntityManager() throws Exception {
    //responsavel pela persistencia
    if (emf != null)
        return emf.createEntityManager();

    return null;
}

public void close() {

    if (emf != null){
        emf.close();
        logger.info("Fechando conexão com BD...");
    }
}

}

Does anyone have any idea what might be happening?

Thank you very much.

    
asked by anonymous 09.09.2016 / 16:37

0 answers