How to persist models entity in playframework subm

2019-02-20 20:02发布

问题:

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 entity when 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

回答1:

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)