0

我正在使用 Play2.11 Java 和 JPA2.0 Hibernate 实现和 mysql

我可以正确运行 Junit 测试用例以在 test/meat-inf 下放置 persistence.xml 的模型上运行 CURD 操作,但是当从网页运行播放到 CURD 时,它显示找不到模型。*类。挖了一段时间还是没有头绪。请帮忙检查一下,非常感谢例外是:

    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: DEVUnit] class or package not found
    at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1416) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
    at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1199) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1063) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:293) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:374) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:71) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
    at prefix.platform.Global.<clinit>(Global.java:8) ~[na:na]
    ... 22 common frames omitted
Caused by: java.lang.ClassNotFoundException: models.Account
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202) ~[na:1.6.0_20]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.6.0_20]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) ~[na:1.6.0_20]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307) ~[na:1.6.0_20]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248) ~[na:1.6.0_20]
    at sbt.PlayCommands$$anonfun$53$$anonfun$55$$anon$2.loadClass(PlayCommands.scala:535) ~[na:na]
    at java.lang.Class.forName0(Native Method) ~[na:1.6.0_20]
    at java.lang.Class.forName(Class.java:247) ~[na:1.6.0_20]
    at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:170) ~[hibernate-core-4.2.0.Final.jar:4.2.0.Final]
    at org.hibernate.ejb.Ejb3Configuration.classForName(Ejb3Configuration.java:1333) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
    at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1405) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final]
    ... 30 common frames omitted

conf/meta-inf/persistence.xml

<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="DEVUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>dev</non-jta-data-source>
    <class>models.Account</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="false" />
    </properties>
</persistence-unit>

测试/meta-inf/persistence.xml

<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="DEVUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>models.Account</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.username" value="***" />
        <property name="hibernate.connection.password" value="***" />
        <property name="hibernate.connection.url"
            value="jdbc:mysql://****.net:3306/dev?characterEncoding=UTF-8" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="false" />
    </properties>
</persistence-unit>

只有一条线不同<non-jta-data-source>dev</non-jta-data-source>

conf/application.conf

# This is the main configuration file for the application.
# ~~~~~

# Secret key
# ~~~~~
# The secret key is used to secure cryptographics functions.
# If you deploy your application to several instances be sure to use the same key!
application.secret="Qk5y/1b1;]>p=yo6?/FnmQR4F@W2lGYWfJI>e4oBs2D0nahNl>Y40y1A:P[uc;RJ"

# The application languages
# ~~~~~
application.langs="en"

# Global object class
# ~~~~~
# Define the Global object class for this application.
# Default to Global in the root package.
#application.global=Global
#ehcacheplugin=disabled
# Router 
# ~~~~~
# Define the Router object to use for this application.
# This router will be looked up first when the application is starting up,
# so make sure this is the entry point. 
# Furthermore, it's assumed your route file is named properly. 
# So for an application router like `conf/my.application.Router`,
# you may need to define a router file `my.application.routes`.
# Default to Routes in the root package (and `conf/routes`)
# application.router=my.application.Routes

# Database configuration
# ~~~~~ 
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://****.net/dev?characterEncoding=UTF-8"
db.default.user=***
db.default.password=****
db.default.jndiName=dev
#
# You can expose this datasource via JNDI if needed (Useful for JPA)
# db.default.jndiName=DefaultDS




# Evolutions
# ~~~~~
# You can disable evolutions if needed
evolutionplugin=disabled

# Logger
# ~~~~~
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .

# Root logger:
logger.root=ERROR

# Logger used by the framework:
logger.play=INFO

# Logger provided to your application:
logger.application=DEBUG
4

1 回答 1

0

我遇到了同样的问题,似乎:我的 JPA 代码(用 Scala 编写,而不是 Java)在 Play(Scala 版本)之外运行良好,但在其中就无法正常工作。我修改了很多persistence.xml 更改,尝试了很多示例,没有骰子。JPA 就是找不到我的实体类。

最终我得到它与以下工作:

  1. 添加sbt.PlayKeys.javaJpa到“appDependencies”中Build.scala(不知道为什么我必须限定它而不是只指定“javaJpa”,但我确实这样做了)
  2. 用于play.db.jpa.JPA.em()创建实体管理器;无论出于何种原因,您都无法自己创建
  3. 添加jpa.name = nameapplication.conf其中“名称”是我的持久性单元的名称

第 3 步花了一些时间才弄清楚,那是通过挖掘 JPA 和 JPAPlugin 的源代码。如果你忽略它,JPA 类会抱怨"No JPA EntityManagerFactory configured for name [whatever]",即使你的persistence.xml文件中定义了“whatever”。 为什么我们需要另一个级别的间接命名我不知道的东西,但是……这最终使它起作用了。

于 2013-09-24T23:41:20.517 回答