|
GettingStarted
Getting Started with SimpleJPA
IntroductionHere's how to get started using SimpleJPA. DependenciesNeed the latest releases of (I'll try to package these up into the release if I can figure out the licensing compatability):
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?
|
Sign in to add a comment
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?'.