Of course, you don't have to agree with me. Everyone has his/her own background/experience, and perspective.
(DRAFT) ... Coming soon ...
Issues:
- double quote for SBT_...."
- Create a blank sbtconfig.txt in conf folder
add mysql connector dependencies to build.sbt
"mysql" % "mysql-connector-java" % "5.1.29",
enable DB
http://www.waitingforcode.com/play-framework/database-and-jpa-in-play-framework/read
http://www.techferry.com/articles/hibernate-jpa-annotations.html#GeneratedValue
Basic Steps:
Install Play
- Install JDK 1.8
- download Play
set PATH=C:\jdk1.8.0_65\bin;C:\activator-1.3.10\bin;C:\Windows\System32
Running into issues:
- findstr cannot be found ==> add C:\Windows\System32 to the PATH
- create an empty config file in conf\ folder: sbtconfig.txt
- C:\RUNNING_PID not found ==> Run command as Admin: activator ui
To Start Activator UI
C:\> activator ui
Create a new Application
C:\> activator new my-first-app play-java
Configure Database in Play
file: conf\application.conf
default.driver = com.mysql.jdbc.Driver
default.url = "jdbc:mysql://localhost/my_play"
default.username = your_db_user
default.password = your_db_password
default.jndiName=MyPlayDS
file: build.sbt:
In order to get JDBC driver library, you will need to include it (and hibernate JPA if you plan to use it) in build.sbt script:
libraryDependencies ++= Seq(
javaJdbc,
"mysql" % "mysql-connector-java" % "5.1.36",
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "5.1.0.Final",
cache,
javaWs
)
CREATE DATABASE IF NOT EXISTS play_store CHARACTER SET utf8 COLLATE utf8_general_ci;
USE 'play_store';
CREATE TABLE IF NOT EXISTS categories (
id INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS products (
id INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
categories_id INT(3) UNSIGNED NOT NULL,
name VARCHAR(100) NOT NULL,
description TEXT NOT NULL,
price DOUBLE(7, 2) NOT NULL,
PRIMARY KEY (id)
);
file: conf/applicaiton.conf
jpa.default=MyPlayPersistenceUnit
Create a new file for persistence configuration: conf/META-INF/persistence.xml
<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="MyPlayPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>MyPlayDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
</properties>
</persistence-unit>
</persistence>
Define JPA Entities (Using Hibernate)
@Entity
@Table (name="country")
public class Country {
private int id;
private String name;
private List<Country> countries;
private List<City> cities;
@Id
@GeneratedValue(strategy= IDENTITY)
@Column(name="id")
public int getId() {
return this.id;
}
@Column(name="name")
public String getName() {
return this.name;
}
@OneToMany(mappedBy="country")
public List<City> getCities() {
return this.cities;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCities(List cities) {
this.cities = cities;
}
@Override
public String toString() {
return "Country {id: " + this.id + ", name: " + this.name + "}";
}
}
Define Service Layer
import play.db.jpa.JPA;
public class CountryService {
public Country getById(int id) {
return JPA.em().find(Country.class, id);
}
}