Skip to content

Commit

Permalink
Made major changes to only depend on fields, split long strings to mu…
Browse files Browse the repository at this point in the history
…lti-attributes, added OFFSET keyword support, and serialize objects to Base64.

WARNING: Had to comment out foreign key and lobkey support due to its dependencies on beans and enhancers. Will re-add them when I have time to do it without beans and enhancers.
WARNING: core/tests will have some commented/removed code, will add proper tests concerning the major changes.

Removed most dependencies on get/set methods, beans, and enhancers.
(There are still some enhancers and beans somewhere, but most of them shouldn't be used)
Long strings get split to multiple attribute/value pairs (most code from appoxy#5 , adapted for more recent changes in SimpleJPA)
Any serializable objects in members will be serialized into Base64 string (uses long strings split change).
Added OFFSET keyword support, which does a count query and uses the next token to get wanted items (as suggested by AWS people).
Added utility constructor in EntityManagerFactoryImp.
SimpleJPA should ignore static fields now.

Updated README.markdown (and fixed some stuff so it's actually readable).

Signed-off-by: jayther <[email protected]>
  • Loading branch information
jayther committed May 3, 2014
1 parent 94af6be commit 7845c21
Show file tree
Hide file tree
Showing 23 changed files with 487 additions and 189 deletions.
129 changes: 71 additions & 58 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#Jayther's SimpleJPA Fork#
**Note**: this is more for myself so it's easy for me to work with JPA-style configurations.

Improvements include only depending on member fields, splitting long strings to multiple attribute/value pairs (from https://github.com/appoxy/simplejpa/pull/5 , adapted to work in SimpleJPA's more recent changes), and serialize objects into Base64 strings (which also use the long string split feature).

**WARNING**: Foreign keys and Lobs support are removed for now to remove dependencies on beans and enhancers. Will re-add support when I have time.

**WARNING**: There are some commented/removed code in the tests. Will add proper tests for the major changes.

#SimpleJPA's new home.#

Discussion Group: http://groups.google.com/group/simplejpa
Expand All @@ -8,42 +17,45 @@ 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):

commons-lang - You can grab all commons libs at http://commons.apache.org/downloads/index.html
commons-collections
commons-logging
commons-codec
cglib-nodep
kitty-cache
ehcache
scannotation
javassist (required for scannotation)
ejb3-persistence-api
AWS SDK for Java
Apache HttpClient
* commons-lang - You can grab all commons libs at http://commons.apache.org/downloads/index.html
* commons-collections
* commons-logging
* commons-codec
* cglib-nodep
* kitty-cache
* ehcache
* scannotation
* javassist (required for scannotation)
* ejb3-persistence-api
* AWS SDK for Java
* Apache HttpClient

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
commons-lang - You can grab all commons libs at http://commons.apache.org/downloads/index.html
commons-beanutils
commons-collections
commons-logging (required for jets3t)
commons-codec (required for jets3t)
Apache HttpClient - (required for jets3t)
typica
jets3t
cglib-nodep - http://sourceforge.net/project/showfiles.php?group_id=56933
ejb3-persistence-api - http://mirrors.ibiblio.org/pub/mirrors/maven2/org/hibernate/ejb3-persistence/1.0.2.GA/
scannotation - http://scannotation.sourceforge.net/
javassist (scannotation) - http://www.csg.is.titech.ac.jp/~chiba/javassist/
kitty-cache - http://code.google.com/p/kitty-cache/
ehcache - http://ehcache.org/

* commons-lang - You can grab all commons libs at http://commons.apache.org/downloads/index.html
* commons-beanutils
* commons-collections
* commons-logging (required for jets3t)
* commons-codec (required for jets3t)
* Apache HttpClient - (required for jets3t)
* typica
* jets3t
* cglib-nodep - http://sourceforge.net/project/showfiles.php?group_id=56933
* ejb3-persistence-api - http://mirrors.ibiblio.org/pub/mirrors/maven2/org/hibernate/ejb3-persistence/1.0.2.GA/
* scannotation - http://scannotation.sourceforge.net/
* javassist (scannotation) - http://www.csg.is.titech.ac.jp/~chiba/javassist/
* kitty-cache - http://code.google.com/p/kitty-cache/
* ehcache - http://ehcache.org/


##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
accessKey = AAAAAAAAAAAAAAAAAAAAAAA
secretKey = SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

For more configuration options, see Configuration.

Now the Code
Expand All @@ -56,48 +68,49 @@ 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;
}

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

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);
MyObject ob = new MyObject();
ob.setName("Scooby doo");
em.persist(ob);

That's it!

##Querying##
See JPAQuery

##Deleting##
MyObject ob...
em.remove(ob);
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();
em.close();
Close the EntityManagerFactory before you shutdown your app
factory.close();
factory.close();
##What Next?##
See all the JPA features currently supported.
Cast your EntityManager to SimpleEntityManager to get more advanced features like asynchronous operations.
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>SimpleJPA</groupId>
<artifactId>SimpleJPA</artifactId>
<version>1.6-SNAPSHOT-aldrinleal</version>
<version>1.6.1-SNAPSHOT-gm</version>
<name>SimpleJPA</name>
<dependencies>
<dependency>
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

17 changes: 4 additions & 13 deletions core/src/main/java/com/spaceprogram/simplejpa/AnnotationInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class AnnotationInfo {

private Annotation[] classAnnotations;
private PersistentProperty idMethod;
private PersistentProperty idProperty;
private Map<String, PersistentProperty> persistentProperties = new HashMap();
private String discriminatorValue;
private String domainName;
Expand All @@ -30,7 +30,7 @@ public void setClassAnnotations(Annotation[] classAnnotations) {
}

public void setIdProperty(PersistentProperty property) {
this.idMethod = property;
this.idProperty = property;
}

public void setDomainName(String domainName) {
Expand All @@ -41,24 +41,15 @@ public Annotation[] getClassAnnotations() {
return classAnnotations;
}

public PersistentProperty getIdMethod() {
return idMethod;
public PersistentProperty getIdProperty() {
return idProperty;
}

public String getDomainName()
{
return domainName;
}

public PersistentProperty addGetter(Method method) {
PersistentMethod persistentMethod = new PersistentMethod(method);
// if we already have an accessor in the list, don't overwrite it
if (persistentProperties.containsKey(persistentMethod.getFieldName())) return persistentProperties.get(persistentMethod.getFieldName());
persistentProperties.put(persistentMethod.getFieldName(), persistentMethod);
if (persistentMethod.isId()) setIdProperty(persistentMethod);
return persistentMethod;
}

public PersistentProperty addField(Field field) {
PersistentField persistentField = new PersistentField(field);
// if we already have an accessor in the list, don't overwrite it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -80,13 +81,19 @@ public AnnotationInfo getAnnotationInfo(Class c) {

private AnnotationInfo getAnnotationInfo(String className) {
AnnotationInfo ai = getAnnotationMap().get(className);
if (ai == null) {
ai = putAnnotationInfo(getClass(className, null));
}
return ai;
}

// I could have used the getAnnotationInfo() method but I am not sure how it will evolve.
// I found that the meaning was more visible using another method.
public AnnotationInfo getAnnotationInfoUsingFullClassName(String fullClassName) {
AnnotationInfo ai = getAnnotationMap().get(fullClassName);
if (ai == null) {
ai = putAnnotationInfo(getClass(fullClassName, null));
}
return ai;
}

Expand Down Expand Up @@ -199,7 +206,7 @@ public AnnotationInfo putAnnotationInfo(Class c) {
putTableDeclaration(ai, c);
putProperties(ai, c);
putMethods(ai, c);
if (ai.getIdMethod() == null) {
if (ai.getIdProperty() == null) {
throw new PersistenceException("No ID method specified for: " + c.getName());
}
putEntityListeners(ai, c);
Expand All @@ -209,6 +216,7 @@ public AnnotationInfo putAnnotationInfo(Class c) {
}

private void putMethods(AnnotationInfo ai, Class c) {
/*
Method[] methods = c.getDeclaredMethods();
for (Method method : methods) {
// logger.fine("method=" + method.getName());
Expand All @@ -221,6 +229,7 @@ private void putMethods(AnnotationInfo ai, Class c) {
if (transientM != null) continue; // we don't save this one
ai.addGetter(method);
}
*/
}


Expand All @@ -239,9 +248,14 @@ private void putProperties(AnnotationInfo ai, Class c) {

private void parseProperty(AnnotationInfo ai, Class c, Field field) {
// TODO add support for OneToOne
/*
if (!field.isAnnotationPresent(Transient.class) && (field.isAnnotationPresent(ManyToMany.class) || field.isAnnotationPresent(OneToMany.class) || field.isAnnotationPresent(ManyToOne.class) || field.isAnnotationPresent(Id.class))) {
ai.addField(field);
}
*/
if (!field.isAnnotationPresent(Transient.class) && (field.getModifiers() & Modifier.STATIC) == 0) {
ai.addField(field);
}
}

private void putTableDeclaration(AnnotationInfo ai, Class<?> c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ public EntityManagerFactoryImpl(String persistenceUnitName, Map props) {
this(persistenceUnitName, props, null, null);
}

/**
* Use this one in web applications, see:
* http://code.google.com/p/simplejpa/wiki/WebApplications
*
* @param persistenceUnitName
* used to prefix the SimpleDB domains
* @param props
* should have accessKey and secretKey
* @param libsToScan
* a set of
*/
public EntityManagerFactoryImpl(String persistenceUnitName, Map props, Set<String> libsToScan) {
this(persistenceUnitName, props, libsToScan, null);
}

/**
* Use this one in web applications, see:
* http://code.google.com/p/simplejpa/wiki/WebApplications
Expand Down
Loading

0 comments on commit 7845c21

Please sign in to comment.