本文介绍了如何使用jpa / hibernate在playframework子模块中保存模型实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



每个子模块都有这样的文件夹结构:




  • 应用:

    • 控制器

    • 模型 b $ b
    • views


      • submodulename.routes / li>

    • build.sbt



    喜欢在文件夹中坚持所有的java类实体: models



    我应该如何配置play framework和/或hibernate entity manager来扫描这个文件夹

    我有一个示例模型类,如下所示:

     包models.common; 

    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.NoResultException;

    import controllers.common.Index;
    import play.data.validation.Constraints;
    导入play.db.jpa.JPA;
    $ b $ @Entity
    public class AppMode {
    public static AppMode getCurrentConfigurationEntry(){
    return JPA.em()。find(AppMode.class,1L);
    }
    //其余代码在这里 - 不重要//
    }

    但在这种状态下,jvm返回一个运行时错误:

    pre $ [IllegalArgumentException:Unknown entity:models.common.AppMode ]

    注:我正在使用play 2.2.1



    我注意到,当我在persistance.xml中设置时,hibernate正确地为AppMode实体
    创建了sql结构:

     < property name =hibernate.hbm2ddl.autovalue =create-drop/> 

    但是当我走得更远时,我得到了下一个错误:

      [IllegalArgumentException:org.hibernate.hql.ast.QuerySyntaxException:MIncident未映射

    虽然我在该类上注解了@Entity。



    这似乎是正确的映射,但我不能做任何像 hsql select JPA.em()。find()这样的实体操作

    解决方案

    我自己解决了这个问题。



    运行时错误: p>

    [IllegalArgumentException:未知实体:models.common.AppMode]
    由Jpa / Hibernate配置引起。问题是Jpa / Hibernate在编译时看到我的实体(标注@Entity),但是在运行时dosn't。要解决这个问题,我必须手动将所有模型类(实体)指向persistance.xml文件中,如下所示:



    /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 =defaultPersistenceUnittransaction-type =RESOURCE_LOCAL>
    < provider> org.hibernate.ejb.HibernatePersistence< / provider>
    <非-jta-data-source> DefaultDS< /非-jta-data-source>
    < class> models.common.AppMode< / class>
    < class> models.Customer< / class>
    < class> models.Complaint< / class>
    <属性>
    < property name =hibernate.hbm2ddl.autovalue =update/>
    < property name =hibernate.dialectvalue =org.hibernate.dialect.H2Dialect/>
    < property name =hibernate.show_sqlvalue =true/>
    < / properties>
    < / persistence-unit>
    < /余辉>

    原文:



    我想知道为什么在主项目(fe: /app/models/User.java )我可以使用注释 @javax。 persistence.Entity (它有效),但是在子项目中(fe: /modules/common/app/models/AppMode.java )它dosnt工作时,注释 @ javax.persistence.Entity 不够,我必须将每个模型类指向persistance.xml文件。



    我认为这是一个playframework中的错误(我检查过2.2.1,2.2.4和2.3.4版本)

    I have project in PLAY FRAMEWORK wich contain few submodules.

    Each submodule has folder structure like this:

    • app:
      • controllers
      • models
      • views
    • conf:
      • submodulename.routes
    • build.sbt

    I would like to persist all java class entities in folder: models.

    How should I configure play framework and/or hibernate entity manager to scan this folders for entities.

    I have example model class as follow:

    package models.common;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.NoResultException;
    
    import controllers.common.Index;
    import play.data.validation.Constraints;
    import play.db.jpa.JPA;
    
    @Entity
    public class AppMode {
        public static AppMode getCurrentConfigurationEntry() {
            return JPA.em().find(AppMode.class, 1L);
        }
    //rest of code here- not important//
    }
    

    but in this state jvm return me a runtime error:

    [IllegalArgumentException: Unknown entity: models.common.AppMode]
    

    NOTE : I'm using play 2.2.1

    I've noticed that hibernate is correctly create sql structure for AppMode entitywhen I set this in persistance.xml:

        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    

    But when I go further I am getting next error:

    [IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: MIncident is not mapped
    

    While I have annotation @Entity on that class.

    It seems to be correct mapped, but I cant do any operation like hsql select or JPA.em().find() on that entities

    解决方案

    I figure it out on my own.

    Runtime error:

    [IllegalArgumentException: Unknown entity: models.common.AppMode]Is caused by Jpa/Hibernate configuration. Problem is that Jpa/Hibernate sees my entities (mark by annotation @Entity) while compilation, but in runtime dosn't. To fix that I've to manually point all my models class (entitites) in persistance.xml file as follow:

    /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="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <non-jta-data-source>DefaultDS</non-jta-data-source>
                <class>models.common.AppMode</class>
                <class>models.Customer</class>
                <class>models.Complaint</class>
            <properties>
                <property name="hibernate.hbm2ddl.auto" value="update"/>
                <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
                <property name="hibernate.show_sql" value="true"/>
            </properties>
        </persistence-unit>
    </persistence>
    

    Original post:package names (namespaces) in subproject classes in Play Framework

    I wonder why in main project (f.e: /app/models/User.java) I can use annotation @javax.persistence.Entity (and it works), but in sub project (f.e: /modules/common/app/models/AppMode.java) it dosnt work, the annotation @javax.persistence.Entity is not enought, I have to point each model class in persistance.xml file.

    I think it is a bug in playframework (I've checked 2.2.1, 2.2.4, and 2.3.4 versions)

    这篇关于如何使用jpa / hibernate在playframework子模块中保存模型实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:33