My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GettingStarted  
Getting Started with SimpleJPA
Featured, Phase-Support
Updated Sep 9, 2010 by no...@amazon.com

Introduction

Here's how to get started using SimpleJPA.

Dependencies

Starting with version 1.5 SimpleJPA switched to use Amazon's Java SDK. Use the latest releases of (I'll try to package these up into the release if I can figure out the licensing compatability):

When building from source and using the Maven pom.xml file Kitty-cache will need to be added explicitly as a reference.

Dependencies for versions pre 1.5

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 silentcr...@gmail.com, 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 project member 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@gmail.com, 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 3, 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 olos...@gmail.com, Sep 1, 2009

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

Comment by pete...@gmail.com, Jan 13, 2010

ehcache.jar needs to be added to the dependency list unless i'm missing something. i got a runtime exception even though i wasn't using it (noclassdeffound).

Comment by guyava...@gmail.com, Jan 13, 2010

Does it work with AppEngine??

Comment by lsa...@gmail.com, May 4, 2010

This is so cool! Any thoughts on "schema evolution" (aka migration|upgrades)? Example: you define your object and data model for v0.9 and make it available as a beta. Then you make enhancements and changes to the object and data model for v1.0. But now you have a ton of user data already in the v0.9 "schema". What would you recommend as an approach to "upgrade" the "database" to the new v1.0 code? Do we need to know the internals of your "ORM" (or is it Object-SimpleDB-Mapping "OSDBM") implementation to do this correctly?

Comment by christia...@gmail.com, Aug 18, 2010

After sseing the many dependencies I thought I would never get this to work. But it works perfectly without any problems. Very good work, guys!

Comment by menno.va...@gmail.com, Aug 23, 2010

Any plans on using the AWS SDK for Java instead of typica+jets3t to interact with SimpleDB and S3? This would shorten the list of dependencies and increase maintainability.

Comment by christia...@gmail.com, Sep 2, 2010

When I want to use EhCache? I alway get an "java.lang.NoClassDefFoundError?: Could not initialize class net.sf.ehcache.CacheManager?" even when I added ehcache-2.2.0 by maven... ?

Comment by mathieu.debrito, Apr 22, 2011

hi guys ! What do you mean by "Create a file called simplejpa.properties and put on the classpath." ?? I created a file called simplejpa.propertie, I put it in a source package, but what about classpath ?

Thanks and great job !

Comment by j...@highvolumeseller.com, May 27, 2011

This project bills itself as stable, but that was not my experience. The library resorted to all this tomfoolery and logs all these messages about S3 not being available and doing retries. But, when I write my own HTTP-based DAOs into S3, I don't come across those problems.

Comment by kentaro....@vccorp.net, Feb 27, 2012

I got "java.lang.NoClassDefFoundError?" with commons-lang3-3.1.jar. (It doesn't include "org.apache.commons.lang.NotImplementedException?".) commons-lang-2.6.jar works well.


Sign in to add a comment
Powered by Google Project Hosting