1

I am trying to create multi submodule project in PlayFramework (play framework 2.2.1).

How should I name package in submodules?

F.E. I have structure as follow:

  • app
  • conf
  • doc
  • logs
  • modules:
  • common:
    • app:
      • controllers
      • models
      • views
    • conf:
      • common.routes
    • build.sbt
  • project
  • public
  • target
  • test

How package (namespace) I should named in file:

/modules/common/app/controllers/Aplication.java

Which one should it be:

package controllers;
package controllers.common;
package common.app.controllers;

actually I have like that:

package controllers.common;
public class Index extends Controller {}

The same issue I have with my model classes in submodules. F.e. in file: /modules/common/app/models/User.java

Which one should it be:

package models;
package models.common;
package common.app.models;

actually I have like that:

package models.common;
public class User {}

runtime error that I get:

[IllegalArgumentException: Unknown entity: models.Cart]
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
masterdany88
  • 5,041
  • 11
  • 58
  • 132
  • You are naming the folder 'Modules' but it should be handled like 'Sub Projects'? In play you can choose what ever package name you want. There is no restriction as far as I know. This questions lacks a error message? What error do you receive when you use a custom namespace? – adis Sep 17 '14 at 09:42
  • I am getting a runtime error: [IllegalArgumentException: Unknown entity: models.Cart] I dont know why it happens. I thought it is caused by package name. – masterdany88 Sep 17 '14 at 10:30
  • Very unclear question, you have posted 3 package names conventions but which one do you use? Did you tried the other one? Do they all produce the same error message? Please post you project on github. – adis Sep 17 '14 at 15:04
  • I've found that each version with controllers.* and models.* on begining of package name works. – masterdany88 Sep 18 '14 at 06:13
  • Runtime error that I've pointed in question is related with jpa/hibernate. Jpa/hibernate sees my entity when it is compiled. But when I access Entity somehow on runtime it dosnt any more, which cause mentioned runtime error. – masterdany88 Sep 18 '14 at 06:16

1 Answers1

1

I've found solution.

It dosn't matter how I named packages. One requirement from play framework is to start name of package with:

  • controllers - in case of naming controller package (f.e: package controllers.common;)
  • models - in case of naming model package (f.e: package models.common;, or just package models; )

Runtime error:

[IllegalArgumentException: Unknown entity: models.Cart]

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.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>

See more here: How to persist models entity in playframework submodule using jpa/hibernate

Community
  • 1
  • 1
masterdany88
  • 5,041
  • 11
  • 58
  • 132
  • Good job! But remember that that Play does not require package names to begin with. I have working examples where packages begins with `nl.package`. But I have used Ebean instead of Hibernate. – adis Sep 18 '14 at 11:53
  • I've run into another problem. Can You help? http://stackoverflow.com/questions/25912001/playframework-how-to-access-routes-in-view-from-other-submodule-view – masterdany88 Sep 18 '14 at 11:59