Skip to content

Commit

Permalink
added EntityManagerImpl.persist(Collection<Object> objColl)
Browse files Browse the repository at this point in the history
  • Loading branch information
btoddb committed Nov 1, 2011
1 parent 0ddb8ca commit cedaa9c
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 271 deletions.
4 changes: 4 additions & 0 deletions object-mapper/CHANGELOG
Expand Up @@ -8,6 +8,10 @@ Changes by version:
- issue-246 : BREAKS EXISTING ANONYMOUS HANDLERS!!! changed anonymous property handling so the property value can be
any type. Also removed @Anonymous... method annotations in favor of a single
@AnonymousPropertyHandling at class level
- BREAKS EXISTING EntityManager (possibly) : It implemented javax.persistence.EntityManager, which has been removed - none of
the methods were implemented. deprecated save and load in favor of find and persist (because probably popular methods)
- added EntityManagerImpl.persist(Collection<Object> objColl) - the main advantage over persist(Object obj) is that
a single batch mutation will be used for performance


2.0-01
Expand Down
293 changes: 39 additions & 254 deletions object-mapper/src/main/java/me/prettyprint/hom/EntityManagerImpl.java
@@ -1,19 +1,10 @@
package me.prettyprint.hom;

import java.util.Map;
import java.util.Collection;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.Table;

import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.ColumnSlice;
Expand All @@ -29,13 +20,12 @@
*
* @author
*/
public class EntityManagerImpl implements EntityManager {
public class EntityManagerImpl {
private static Logger logger = LoggerFactory.getLogger(EntityManagerImpl.class);

private Keyspace keyspace;
private HectorObjectMapper objMapper;
private ClassCacheMgr cacheMgr;
private boolean open;

public EntityManagerImpl(Keyspace keyspace, String classpathPrefix) {
this(keyspace, new String[] { classpathPrefix }, null, null);
Expand Down Expand Up @@ -68,9 +58,11 @@ public EntityManagerImpl(Keyspace keyspace, String[] classpathPrefix, ClassCache
* Initialize the manager by scanning the classpath starting with the
* <code>classpathPrefix</code>, looking for classes annotated with
* {@link Entity}. If an Entity class is found, it looks for the
* {@link BasicTable} annotation to determine the Cassandra column family
* {@link Table} annotation to determine the Cassandra column family
* name.
*
* @see ClassCacheMgr
*
* @param classpathPrefixArr
*/
public void initialize(String[] classpathPrefixArr) {
Expand All @@ -83,7 +75,6 @@ public void initialize(String[] classpathPrefixArr) {
logger.debug("classpath array has {} items : {}",
(null != classpathPrefixArr ? classpathPrefixArr.length : "0"), classpathPrefixArr);
}
open = true;
}

private void initializeClasspath(String classpathPrefix) {
Expand All @@ -94,6 +85,21 @@ private void initializeClasspath(String classpathPrefix) {
}
}

@Deprecated
public <T, I> T load(Class<T> clazz, I id) {
return find(clazz, id);
}

@Deprecated
public <T> T load(Class<T> clazz, Object id, ColumnSlice<String, byte[]> colSlice) {
return find(clazz, id, colSlice);
}

@Deprecated
public <T> T save(T obj) {
return persist(obj);
}

/**
* Load an entity instance. If the ID does not map to a persisted entity, then
* null is returned.
Expand All @@ -108,7 +114,7 @@ private void initializeClasspath(String classpathPrefix) {
* ID of the instance to load
* @return instance of Entity or null if can't be found
*/
public <T, I> T load(Class<T> clazz, I id) {
public <T, I> T find(Class<T> clazz, I id) {
if (null == clazz) {
throw new IllegalArgumentException("clazz cannot be null");
}
Expand All @@ -124,7 +130,7 @@ public <T, I> T load(Class<T> clazz, I id) {

return objMapper.getObject(keyspace, cfMapDef.getEffectiveColFamName(), id);
}

/**
* Load an entity instance given the raw column slice. This is a stop gap
* solution for instanting objects using entity manager while iterating over
Expand All @@ -141,7 +147,7 @@ public <T, I> T load(Class<T> clazz, I id) {
* <code>ColumnSlice<String, byte[]></code>
* @return Completely instantiated persisted object
*/
public <T> T load(Class<T> clazz, Object id, ColumnSlice<String, byte[]> colSlice) {
public <T> T find(Class<T> clazz, Object id, ColumnSlice<String, byte[]> colSlice) {
if (null == clazz) {
throw new IllegalArgumentException("clazz cannot be null");
}
Expand All @@ -158,255 +164,34 @@ public <T> T load(Class<T> clazz, Object id, ColumnSlice<String, byte[]> colSlic
T obj = objMapper.createObject(cfMapDef, id, colSlice);
return obj;
}

/**
* Save the entity instance.
*
* @param <T>
* @param obj
* @return
*/
public <T> T save(T obj) {
public <T> T persist( T obj ) {
if (null == obj) {
throw new IllegalArgumentException("object to save cannot be null");
}
return objMapper.saveObj(keyspace, obj);
}

@Override
public void persist(Object entity) {
save(entity);
}

@Override
public <T> T find(Class<T> entityClass, Object primaryKey) {
return load(entityClass, primaryKey);
}

@Override
public void clear() {
throw new RuntimeException("Method is not implemented");

}

@Override
public void close() {
throw new RuntimeException("Method is not implemented");

}

@Override
public boolean contains(Object entity) {
throw new RuntimeException("Method is not implemented");
// return false;
}

@Override
public Query createNamedQuery(String name) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public Query createNativeQuery(String sqlString) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public Query createNativeQuery(String sqlString, @SuppressWarnings("rawtypes") Class resultClass) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public Query createNativeQuery(String sqlString, String resultSetMapping) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public Query createQuery(String qlString) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public void flush() {
throw new RuntimeException("Method is not implemented");

}

@Override
public Object getDelegate() {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public FlushModeType getFlushMode() {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public <T> T getReference(Class<T> entityClass, Object primaryKey) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public EntityTransaction getTransaction() {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public boolean isOpen() {
return open;
}

@Override
public void joinTransaction() {
throw new RuntimeException("Method is not implemented");

}

@Override
public void lock(Object entity, LockModeType lockMode) {
throw new RuntimeException("Method is not implemented");

}

@Override
public <T> T merge(T entity) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public void refresh(Object entity) {
throw new RuntimeException("Method is not implemented");

}

@Override
public void remove(Object entity) {
throw new RuntimeException("Method is not implemented");

}

@Override
public void setFlushMode(FlushModeType flushMode) {
throw new RuntimeException("Method is not implemented");

}

@Override
public <T> TypedQuery<T> createNamedQuery(String arg0, Class<T> arg1) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public <T> TypedQuery<T> createQuery(CriteriaQuery<T> arg0) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public <T> TypedQuery<T> createQuery(String arg0, Class<T> arg1) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public void detach(Object arg0) {
throw new RuntimeException("Method is not implemented");

}

@Override
public <T> T find(Class<T> arg0, Object arg1, Map<String, Object> arg2) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public <T> T find(Class<T> arg0, Object arg1, LockModeType arg2) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public <T> T find(Class<T> arg0, Object arg1, LockModeType arg2, Map<String, Object> arg3) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public CriteriaBuilder getCriteriaBuilder() {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public EntityManagerFactory getEntityManagerFactory() {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public LockModeType getLockMode(Object arg0) {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public Metamodel getMetamodel() {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public Map<String, Object> getProperties() {
throw new RuntimeException("Method is not implemented");
// return null;
}

@Override
public void lock(Object arg0, LockModeType arg1, Map<String, Object> arg2) {
throw new RuntimeException("Method is not implemented");

}

@Override
public void refresh(Object arg0, Map<String, Object> arg1) {
throw new RuntimeException("Method is not implemented");

}

@Override
public void refresh(Object arg0, LockModeType arg1) {
throw new RuntimeException("Method is not implemented");

}

@Override
public void refresh(Object arg0, LockModeType arg1, Map<String, Object> arg2) {
throw new RuntimeException("Method is not implemented");

}

@Override
public void setProperty(String arg0, Object arg1) {
throw new RuntimeException("Method is not implemented");

/**
* Save the list of entity intances.
*
* @param objColl
* @return
*/
public Collection<Object> persist(Collection<Object> objColl) {
if (null == objColl) {
throw new IllegalArgumentException("object to save cannot be null");
}

objMapper.saveObjList(keyspace, objColl);
return objColl;
}

@Override
public <T> T unwrap(Class<T> arg0) {
throw new RuntimeException("Method is not implemented");
// return null;
}
}

0 comments on commit cedaa9c

Please sign in to comment.