简体   繁体   中英

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

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM