|
GettingStarted
IntroductionHere's how to get started using SimpleJPA. DependenciesStarting 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 SimpleJPAhttp://code.google.com/p/simplejpa/downloads/list SetupCreate 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 CodeCreate 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 objectLets 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! QueryingSee JPAQuery DeletingMyObject ob... em.remove(ob); Close the EntityManager when you're doneThis 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 appfactory.close(); What Next?
|
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..
silentcrooner: check out this page for details on using in an app server. In particular, you might want to look at SimpleJPAUtil.
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.
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
I think that link to {WebApplications} page would be very usefull here and should be added into 'What Next?'.
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).
Does it work with AppEngine??
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?
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!
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.
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... ?
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 !
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.
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.