0

I'm currently in the process of learning the Play framework, but ran into some trouble pretty quickly.

I've got an independent Java project that uses Hibernate to persist and fetch data from a MySQL database. This project is full of DTOs and DAOs.

I thought it would be a good idea to reference the Hibernate project in my new Play framework project, and consequently take advantage of the different DAOs and DTOs through it.

Everything compiles in Eclipse, but when running my Play application in the browser I get an error that the package com.path.to.hibernate.project.package could not be found.

Is there a secret way to reference other projects in the Play framework or am I simply doing it wrong? I kind of like the idea of separating the Hibernate stuff from the Play framework stuff.

If you think I should be taking a different approach on this, I'm all ears.

Thanks.

EDIT:

build.sbt (equivalent of Build.scala?)

name := "klepp_rc_play"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  "mysql" % "mysql-connector-java" % "5.1.29"
)     

play.Project.playJavaSettings

application.conf

...
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/play"
db.default.user=****
db.default.password=****

ebean.default="models.*"
...

conf/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.MySQL5Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>
    </persistence-unit>

</persistence>
Morten Salte
  • 505
  • 5
  • 21
  • Show us some config files. At least a `Build.scala`. This seems to me an `sbt` issue. – Balázs Németh Mar 02 '14 at 23:35
  • Thanks for your response. I have now edited in several config files. As far as I can tell, build.sbt is the equivalent of Build.scala? – Morten Salte Mar 03 '14 at 06:29
  • It's roughly the same. – Balázs Németh Mar 03 '14 at 08:50
  • 1
    Probably you set the dependency in eclipse on your project but you haven't set in `sbt`. You have to tell your application about your local module. See for example this answer: http://stackoverflow.com/questions/8330149/in-sbt-how-do-i-specify-a-project-dependency-on-another-possibly-independent-s – Balázs Németh Mar 03 '14 at 09:50
  • Thanks again. I can see from that example that Build.scala files are used. Can I have a Build.scala file like that in addition to my build.sbt file? If not would you please give me an example of how to define root projects in a build.sbt friendly manner? – Morten Salte Mar 03 '14 at 10:25

1 Answers1

1

Your question involves some knowledge of sbt, I wouldn't even say it's just basic stuff. You ideally want your independent project built with sbt and then referenced from your play application.

Here's what I would do as a start, and then as you progress with sbt, you could refine this.

A, Build your independent project they way the most comfortable is for you. This may even mean you just export a jar file from eclipse (yes, some would consider this as an act of the devil, but we just want to kick-start this). But pay attention to all of its dependencies! Then take this jar and put it in your play application's lib folder (if there's no such folder, create one). It will be automatically put on the class path. Now you can either put your dependencies into this folder as well, or list them in your build.sbt.

B, If you built your application with maven, you could reference your project in build.sbt as usual ("groupId" % "artifactId" % "version"). But you should add your local repository to your build.sbt. Here's how to do it: SBT doesn't find file in local maven repository although it's there

C, Convert your independent project to an sbt project, then you can manage its build process easily.

Now if you want to modify your independent project along the way, it's probably worth to go with option C. Otherwise if you just want to start to get to know Play Framework, go with A or B. But you'll need to get to know sbt as well.

Community
  • 1
  • 1
Balázs Németh
  • 6,222
  • 9
  • 45
  • 60
  • Thanks a bunch. I managed to get option A working. I might be looking into sbt anyway as it sounds much more robust and neat :) – Morten Salte Mar 03 '14 at 14:18