JavaFX和Hiberante - >无法找到持久性单位

时间:2015-10-05 11:15:35

标签: java eclipse hibernate jpa

我创建了一个JavaFX项目作为桌面独立应用程序。 对于持久性,我使用hibernate 5.0.1

当我在Eclipse中运行项目时,一切正常。 使用Ant构建项目会导致以下错误: “javax.persistence.PersistenceException:无法找到持久性单元”

Exception in Application constructor
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(JarRsrcLoa
der.java:58)
Caused by: java.lang.RuntimeException: Unable to construct Application instance:
 class src.main.java.MainApp
        at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So
urce)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(
Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$1/1634198.run(Unknown
 Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$152
(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$44/13272165.run(Unkno
wn Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown
 Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$46/24080292.run(Unkno
wn Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Sourc
e)
        at com.sun.javafx.application.PlatformImpl$$Lambda$48/19739574.run(Unkno
wn Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown S
ource)
        at com.sun.javafx.application.PlatformImpl$$Lambda$47/27265806.run(Unkno
wn Source)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
        at com.sun.glass.ui.win.WinApplication$$Lambda$38/7264602.run(Unknown So
urce)
        ... 1 more
Caused by: javax.persistence.PersistenceException: Unable to locate persistence
units
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactor
yBuilderOrNull(HibernatePersistenceProvider.java:84)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactor
yBuilderOrNull(HibernatePersistenceProvider.java:71)
        at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFac
tory(HibernatePersistenceProvider.java:52)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.
java:55)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.
java:39)
        at src.main.java.dao.TypGen_DAO.<init>(TypGen_DAO.java:22)
        at src.main.java.MainApp.<init>(MainApp.java:53)
        ... 18 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range:
 -1
        at java.lang.String.substring(Unknown Source)
        at org.hibernate.boot.archive.internal.ArchiveHelper.getJarURLFromURLEnt
ry(ArchiveHelper.java:45)
        at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistence
Xml(PersistenceXmlParser.java:105)
        at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(Persis
tenceXmlParser.java:84)
        at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenc
eUnits(PersistenceXmlParser.java:66)
        at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactor
yBuilderOrNull(HibernatePersistenceProvider.java:80)
        ... 24 more

Persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
    <persistence-unit name="jpa-example" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>src.main.model.testClass</class>

        <properties>
            <property name="javax.persistence.jdbc.url"
                value="jdbc:sqlite:\\\\...localnetworkURL\\Database\\TypGen.db"></property>
            <property name="javax.persistence.jdbc.user" value="root"></property>
            <property name="javax.persistence.jdbc.password" value="my_root_password"></property>
            <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC"></property>
            <property name="hibernate.show_sql" value="true"></property>
            <property name="hibernate.format_sql" value="true"></property>

            <property name="hibernate.dialect" value="src.main.java.resources.SQLiteDialect"></property>
            <property name="hibernate.hbm2ddl.auto" value="update"></property>

            <property name="hibernate.c3p0.min_size" value="5" />
            <property name="hibernate.c3p0.max_size" value="20" />
            <property name="hibernate.c3p0.timeout" value="500" />
            <property name="hibernate.c3p0.max_statements" value="50" />
            <property name="hibernate.c3p0.idle_test_period" value="2000" />
        </properties>
    </persistence-unit>
</persistence>

不幸的是我不能在项目中使用maven,我很感激任何帮助:)

1 个答案:

答案 0 :(得分:0)

问题是: 从控制台启动Jar时使用错误的Java版本