My favorites | Sign in
Project Logo
                
Search
for
Updated Aug 02, 2009 by treeder
Labels: Featured, Phase-Support
GettingStarted  
Getting Started with SimpleJPA

Introduction

Here's how to get started using SimpleJPA.

Dependencies

Need the latest releases of (I'll try to package these up into the release if I can figure out the licensing compatability):

Download SimpleJPA

http://code.google.com/p/simplejpa/downloads/list

Setup

Create a file called simplejpa.properties and put on the classpath. Add your Amazon access key and secret key like:

accessKey = AAAAAAAAAAAAAAAAAAAAAAA
secretKey = SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

For more configuration options, see Configuration.

Now the Code

Create an EntityManagerFactory

// Create EntityManagerFactory. This should be a global object that you reuse.
private static EntityManagerFactoryImpl factory = new EntityManagerFactoryImpl("persistenceUnitName", null);

Get EntityManager's from the Factory

// Get an EntityManager from the factory. This is a short term object that you'll use for some processing then throw away
EntityManager em = factory.createEntityManager();

Persisting an object

Lets create a very simple object to store.

@Entity
public class MyTestObject {
    private String id;
    private String name;

    @Id
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
   
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

Now to persist it:

MyObject ob = new MyObject();
ob.setName("Scooby doo");
em.persist(ob);

That's it!

Querying

See JPAQuery

Deleting

MyObject ob...
em.remove(ob);

Close the EntityManager when you're done

This is done after you've completed a set of tasks, such as displaying a web page. It ensures that caches get cleaned up and no memory gets wasted.

em.close();

Close the EntityManagerFactory before you shutdown your app

factory.close();

What Next?


Comment by silentcrooner, Mar 23, 2008

Any thoughts on how this could be done via resource inclusion and the @PersistenceContext? annotation?

I assume this involves changing the JPA provider in my App Server, and configuring SimpleJPA as its JPA implementation. Is this the only way to go in an app server context?

Until then I guess it's the old EJB/JDBC antipattern of overriding the lifecycle methods of Stateless Session Beans and creating and closing EMs in these..

Comment by treeder, May 14, 2008

silentcrooner: check out this page for details on using in an app server. In particular, you might want to look at SimpleJPAUtil.

Comment by r.g.siebeck, Mar 19, 2009

Do not use typica Version 1.5.0 - there's a bug preventing you to do queries. Instead check out the SVN version or use 1.5.1.

Comment by jwusch, Apr 03, 2009

1.5.1 of typica is having problems with adding rows to the database. I am getting an exception; java.lang.NoSuchMethodError?: com.xerox.amazonws.sdb.Item.putAttributes(Ljava/util/List;)V

when I try to persist an object. It was working before I upgraded typica

Comment by olostan, Sep 01, 2009

I think that link to {WebApplications} page would be very usefull here and should be added into 'What Next?'.


Sign in to add a comment
Hosted by Google Code