Development of Flex Blogging system from zero based on FLEX4 technology: 7 Using JPA

Source: Internet
Author: User

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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.