如何在可执行jar文件中使用JPA?

时间:2013-09-16 19:21:03

标签: java jpa jar jpa-2.0 executable-jar

我有这个结构:

  • xptoapp
    • xptoapp-主
    • xptoapp核
    • xptoapp域

包'xptoapp-main'有一个像这样的pom.xml:

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


在项目'xptoapp-domain'中,我有一个META-INF / persistence.xml。我应该在项目'xptoapp-app'(文件夹META-INF)中放置一个persistence.xml吗?

当我尝试'java -jar xpto-app.jar'时,我有一个例外:

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



我如何在我的案例中使用JPA?为什么我得到这个例外?

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

1 个答案:

答案 0 :(得分:0)

固定。问题出在eclipse出口我的罐子的那一刻。它将文件夹META-INF放在名为“resources”的文件夹中。

谢谢大家。