APP engine Java supports not only JDO but also JPA in data storage. 4th, 5 lesson we are storing data using JDO, this lesson take a look if using JPA.
One, Using JPA
Open the Gapp_flexblog project, and in the src/meta-inf/directory, add the Persistence.xml file, which reads as follows:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="transactions-optional">
<provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
</properties>
</persistence-unit>
</persistence>
Where Persistence-unit's name is used when instantiating entitymanagerfactory.
In the Sban.flexblog.managers directory, add Class Emfactory:
package sban.flexblog.managers;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public final class EMFactory {
private static EntityManagerFactory _instance = Persistence
.createEntityManagerFactory("transactions-optional");
private EMFactory() {
}
public static EntityManagerFactory getInstance() {
return _instance;
}
}
This class is used to return an instance of entitymanagerfactory where the parameter "transactions-optional" is defined above.
Add entity class greetingentity to the Sban.flexblog directory:
Package sban.flexblog;
Import Java.util.Date;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
@Entity
public class Greetingentity {
Public greetingentity (string user, string content, date date)
{
This.user = user;
This.greetingcontent=content;
This.date = date;
}
@Id
@GeneratedValue (strategy = generationtype.identity)
Private Long ID;
Private String user;
Private String greetingcontent;
private date date;
Public Long getId ()
{
return this.id;
}
Public String getgreetingcontent ()
{
return this.greetingcontent;
}
public void Setgreetingcontent (String v)
{
This.greetingcontent = v;
}
Public Date getDate ()
{
return this.date;
}
}