Skip to content

play 2.1 sample application which show how to connect two different databases with the use of two different persistenceUnits.

Notifications You must be signed in to change notification settings

cm0s/play2-jpa-multiple-persistenceunit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# Play framework app + JPA with multiple persistenceUnit / DB

This is a play 2.1 sample application which show how to connect two different databases with the use of two
different persistenceUnits.

## What this sample do?
There is three actions in the Application controller which create and persist a User and a Company object in a H2
database with the use of the "defaultPersistenceUnit" and the "otherPersistenceUnit" declared in the persistence.xml
file.

In test1 and test2 JPA.em().persist() is directly used in the action and in the test3 JPA.em().persist() is used inside
the Entity(Model) classes.

When JPA.em().persist() is used inside an action the correct Transaction has to be specified manually (see test1 and 2),
however when JPA.em().persist() is used directly in one of the Model's methods (see test3) the correct persistence
should be used based on what is declared in the config files. But it's not. So the test3 generate an error :
    IllegalArgumentException: Unknown entity: models.Company
This error is generated because the JPA entity manager is not capable to use a transaction with the otherPersistenceUnit
declared in the persistence.xml.

## How to use this sample?
1. Clone this project
2. Start the application ($play run)
3. Use http://localhost:9000/test1, http://localhost:9000/test2 to run one of the working example.
3. Use http://localhost:9000/test3 to run the not working example.

A User and a Company object is persisted in the H2 memory database.

To browse the H2 database, use the $h2-browser command in the play console.

H2 databases can be replaced by other databases like MySQL. Just modify it in the application.conf and set the correct
hibernate.dialect in the persistence.xml file.


About

play 2.1 sample application which show how to connect two different databases with the use of two different persistenceUnits.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published