5

I cannot somehow import the play.db.jpa.Model lib into my Model, it can only find play.db.ebean.Model. Is there any way to import this?

thanks

2 Answers 2

9

That is because play.db.jpa.Model is Play 1.x and play.db.ebean.Model is Play 2.x.

To use JPA in Play 2.x, see http://www.playframework.org/documentation/2.0/JavaJPA

3
  • oh really? but why cannot i find the find() method of Model then?
    – doniyor
    Jun 28, 2012 at 19:05
  • 1
    now do i have to implement the find method on my own? like.. public static User find(Long id){ return JPA.em().find(User.class, id); }
    – doniyor
    Jun 28, 2012 at 19:12
  • 1
    yes...its one of the pros/cons of Play 2.x...its less magic, so more work, but because its less magic, its clearer whats going on.
    – Codemwnci
    Jun 28, 2012 at 19:23
4

The class play.db.jpa.Model isn't available from play 2.0 on. The solution I found to this problem is:

  • In YourProject/conf/application.conf, declare the JPA configuration:

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
db.default.jndiName=DefaultDS

And the persistence unit:

jpa.default=defaultPersistenceUnit

  • Create a new Package named META-INF at YourProject/conf/

  • Inside this package create a file named 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>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            </properties>
        </persistence-unit>


</persistence>
  • At YourProject/project/Build.scala modify the dependencies to add the following:

val appDependencies = Seq(
// Add your project dependencies here,
javaCore,
javaJdbc,
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "4.2.1.Final"
)

Also you can disable Ebean if you aren't going to use it with:

val main = play.Project(appName, appVersion, appDependencies).settings(
  ebeanEnabled := false 
)
  • After this, run play eclipse on your project and refresh.

Hope this helps.

4
  • "jpa.default=defaultPersistenceUnit" in application.conf is the part I spent a few hours trying to figure out. Still haven't found it documented anywhere but in the source to JPAPlugin. :( Sep 24, 2013 at 23:28
  • @RodneyGitzel its in the 2.2 docs under JPA: playframework.com/documentation/2.2.x/JavaJPA , see the last part of "Creating a Persistence Unit"
    – chris
    Oct 19, 2013 at 23:58
  • 2
    @chris unfortunately it was left out of the 2.1 docs: playframework.com/documentation/2.1.x/JavaJPA Oct 22, 2013 at 18:54
  • @RodneyGitzel oh, what a bummer. +1 - hopefully this will save someone else their time / sanity!
    – chris
    Oct 22, 2013 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.