daydreamer
daydreamer

Reputation: 92089

Play! Framework - Using MySQL and MongoDB for same application

Is it possible to user MySQL Database and MongoDb database for same project using Play! framework?

for example: I want

@Entity Person to interact with my MySQL database and  
@Entity PersonData to interact with my MongoDB database?  

How can I do that?

Please let me know
Thank you

Upvotes: 8

Views: 3268

Answers (2)

Ryan
Ryan

Reputation: 1360

Yes, it is possible. Just use the Morphia plugin for Play. I have done it before. It is quite simple.

For the MongoDB models, just do something like this:

import play.modules.morphia.Model;

@Entity
public class YourMongoModel extends Model {
   ...
}

For the MySQL model, do this:

import play.db.jpa.Model;

@Entity
public class LogMessageX extends Model {
  ...
}

Notice the different imports.

Then the application.conf file should contains something like this:

# For MongoDB
morphia.db.host=localhost
morphia.db.port=27017
morphia.db.name=YourMongoDBName

# for MySQL
db=mysql:user:pwd@database_name

Upvotes: 5

Felipe Oliveira
Felipe Oliveira

Reputation: 1041

On the MySQL entity extend Model and add the JPA annotation (@Entity).

For Mongo you need to use a third-party module such as this one: http://www.playframework.org/modules/mongo-1.3/home

Example:

@MongoEntity("collectionName")

public class Car extends MongoModel {

public String name;
public String colour;
public int topSpeed;

}

Play's JPA plugin will not modify the Mongo class since it won't have the JPA @Entity annotation.

For anyone out there interested, checkout Play's JPAEnhancer. It uses javaassist to modify the bytecode and add all the method impls - very cool!

Upvotes: 2

Related Questions