简体   繁体   中英

How to use JPA inside an executable jar file?

I have this structure:

  • xptoapp
    • xptoapp-main
    • xptoapp-core
    • xptoapp-domain

The package 'xptoapp-main' have a pom.xml like this:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org        /xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org    /2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.xpto</groupId>
    <artifactId>xptoapp</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>xptoapp-main</artifactId>
<name>xptoapp-main</name>
<url>http://maven.apache.org</url>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>com.xpto</groupId>
        <artifactId>xptoapp-domain</artifactId>
        <version>${version}</version>
        <scope>provided</scope>
    </dependency> 
    <dependency>
        <groupId>com.xpto</groupId>
        <artifactId>xptoapp-core</artifactId>
        <version>${version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
</project>


Inside the project 'xptoapp-domain' i have a META-INF/persistence.xml. Should i put a persistence.xml inside the project 'xptoapp-app' ( folder META-INF )?

When i try to do 'java -jar xpto-app.jar' i got an exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: javax.persistence.PersistenceException: Unable to configure EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:385)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.xpto.domain.persistence.PersistenceManager.<init>(PersistenceManager.java:14)
    at com.xpto.app.App.main(App.java:30)
    ... 5 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at org.hibernate.ejb.packaging.JarVisitorFactory.getJarURLFromURLEntry(JarVisitorFactory.java:57)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:351)
    ... 10 more



How can i use JPA in my case? Why am i getting this exception?

persistence.xml

<?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="xptoapp"  transaction-type="RESOURCE_LOCAL">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <class>com.xpto.domain.entity.Account</class>
                <class>com.xpto.domain.entity.Transfer</class>
                <properties>
                        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
                        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:xptoapp" />
                        <property name="javax.persistence.jdbc.user" value="sa" />
                        <property name="javax.persistence.jdbc.password" value="" />
                        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
                        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                        <property name="hibernate.show_sql" value="false" />
                </properties>
        </persistence-unit>
</persistence>

Fixed. The problem was in the moment that eclipse exports my jar. It was putting the folder META-INF inside a folder called "resources".

Thanks all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM