Play2.11 Play无法找到模型类,但我可以正常运行纯JPA Junit测试

问题描述 投票:0回答:1

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

我可以正确运行Junit测试用例,在将persistence.xml置于test / meat-inf下的模型上运行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
jpa model playframework-2.0
1个回答
0
投票

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

最终我让它与以下工作:

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

第3步花了一段时间才弄清楚,那是通过挖掘JPA和JPAPlugin的来源。如果你把它留下来,JPA类会抱怨"No JPA EntityManagerFactory configured for name [whatever]",即使你的persistence.xml文件中定义的“无论什么”。为什么在命名我不知道的事情时需要另一层次的间接,但......最终使它成功。

© www.soinside.com 2019 - 2024. All rights reserved.