This repository was archived by the owner on Nov 25, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Using MySQL DB
Mattias Levin edited this page Dec 16, 2016
·
9 revisions
This section will describe how to use MySQL from your Play app.
It assumes you already have a running projects use H2 DB.
- Download MySQL https://dev.mysql.com/downloads/mysql/ (download the DMG file)
- Install the file and follow the instructions NOTE! Write down the random root password shown
- Open System Preferences, there should be new option "MySQL" that lets you start and stop the DB
- Download Sequel Pro (drag to Application folder)
- Open the Sequel Pro and connect to the database
Select Socket tab
Name: localhost
Username: root
Passwork: THE TEMP PASSWORD
Press connect
- You need to change the temporary password do something permanent (have not tried this in Sequel Pro). DO NOT FORGET IT
- Create a database, e.g. "library"
You can continue creating the Book table and columns manually using Sequel Pro or we can let our PlayApp create the schema automatically when we start our server.
- Update settings in "application.config"
# Default database configuration using MySQL database engine
default.driver=com.mysql.jdbc.Driver
default.url="jdbc:mysql://localhost/library"
default.username=root
default.password="YOUR DB ROOT PASSWORD"
NOTE! You need to remove the H2 configuration.
- Update your
persistence.xml
file
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
This will create your schema from your Entity annotations the first time the Play server is started.
- Add MySQL JDBC drive as dependency to your project. Update
build.sbt
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "5.1.0.Final",
"mysql" % "mysql-connector-java" % "5.1.36"
)
- Compile your app
activator compile
- Start your app
activator run
- Look in the logs for any errors
- Make any request to your app (to compile the app)
- Look in Sequel Pro and you
library
BD, you should see two new tables (Books an hibernate_sequence) - Stop the server
- Update your
persistence.xml
file to NOT drop the tables as startup each time - Start your server again
- Create some books and use Sequel Pro to inspect the data