use of siena.SienaException in project siena by mandubian.
the class BaseTest method testTransactionInsertBatchFailure.
public void testTransactionInsertBatchFailure() {
TransactionAccountFrom accFrom = new TransactionAccountFrom(1000L);
TransactionAccountTo accTo = new TransactionAccountTo(1000L);
try {
pm.beginTransaction(Connection.TRANSACTION_READ_COMMITTED);
accFrom.amount = 1000L;
accTo.amount = 100L;
pm.insert(accFrom, accTo);
throw new SienaException("test");
} catch (SienaException e) {
pm.rollbackTransaction();
} finally {
pm.closeConnection();
}
TransactionAccountFrom accFromAfter = pm.getByKey(TransactionAccountFrom.class, accFrom.id);
assertNull(accFromAfter);
TransactionAccountTo accToAfter = pm.getByKey(TransactionAccountTo.class, accTo.id);
assertNull(accToAfter);
}
use of siena.SienaException in project siena by mandubian.
the class GaeMappingUtils method mapRelation.
public static <T> T mapRelation(Query<T> query, T obj, ClassInfo info) {
// aggregators/owners
List<QueryAggregated> aggregs = query.getAggregatees();
List<QueryOwned> ownees = query.getOwnees();
if (aggregs.isEmpty() && ownees.isEmpty()) {
return obj;
}
if (aggregs.size() == 1) {
// aggregators
QueryAggregated aggreg = aggregs.get(0);
Relation rel = new Relation(RelationMode.AGGREGATION, aggreg.aggregator, aggreg.field);
Util.setField(obj, info.aggregator, rel);
} else if (aggregs.size() > 1) {
throw new SienaException("Only one aggregation per query allowed");
}
if (ownees.size() == 1) {
// owners
QueryOwned ownee = ownees.get(0);
Util.setField(obj, ownee.field, ownee.owner);
} else if (ownees.size() > 1) {
throw new SienaException("Only one owner per query allowed");
}
return obj;
}
use of siena.SienaException in project siena by mandubian.
the class GaeMappingUtils method fillModelAndKey.
public static void fillModelAndKey(Object obj, Entity entity) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Field id = info.getIdField();
Class<?> fieldClass = id.getType();
Key key = entity.getKey();
if (key != null) {
setIdFromKey(id, obj, key);
}
for (Field field : info.updateFields) {
String property = ClassInfo.getColumnNames(field)[0];
try {
fieldClass = field.getType();
if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) {
/*if(!ClassInfo.isAggregated(field)){*/
key = (Key) entity.getProperty(property);
if (key != null) {
Object value = Util.createObjectInstance(fieldClass);
id = ClassInfo.getIdField(fieldClass);
setIdFromKey(id, value, key);
Util.setField(obj, field, value);
}
/*}*/
} else if (ClassInfo.isEmbedded(field) && field.getAnnotation(Embedded.class).mode() == Embedded.Mode.NATIVE) {
Object value = GaeNativeSerializer.unembed(field.getType(), ClassInfo.getSingleColumnName(field), entity, 0);
Util.setField(obj, field, value);
} else /*else if(ClassInfo.isAggregated(field)){
// does nothing for the time being
}
else if (ClassInfo.isOwned(field)){
// does nothing for the time being
}*/
{
setFromObject(obj, field, entity.getProperty(property));
}
} catch (Exception e) {
throw new SienaException(e);
}
}
}
use of siena.SienaException in project siena by mandubian.
the class GaePersistenceManager method mapJoins.
protected <T> List<T> mapJoins(List<T> models) {
try {
// join queries
Map<Field, ArrayList<Key>> fieldMap = null;
// creates the list of joined entity keys to extract
for (final T model : models) {
// initializes fieldMap
if (fieldMap == null) {
fieldMap = GaeQueryUtils.buildJoinFieldKeysMap(model);
}
for (Field field : fieldMap.keySet()) {
Object objVal = Util.readField(model, field);
// our object is not linked to another object...so it doesn't have any key
if (objVal == null) {
continue;
}
Key key = GaeMappingUtils.getKey(objVal);
List<Key> keys = fieldMap.get(field);
if (!keys.contains(key))
keys.add(key);
}
}
Map<Field, Map<Key, Entity>> entityMap = new HashMap<Field, Map<Key, Entity>>();
try {
// retrieves all joined entities per field
for (Field field : fieldMap.keySet()) {
Map<Key, Entity> entities = ds.get(fieldMap.get(field));
// gets the future here because we need it so we wait for it
entityMap.put(field, entities);
}
} catch (Exception ex) {
throw new SienaException(ex);
}
// associates linked models to their models
// linkedModels is just a map to contain entities already mapped
Map<Key, Object> linkedModels = new HashMap<Key, Object>();
Object linkedObj;
Entity entity;
for (final T model : models) {
for (Field field : fieldMap.keySet()) {
Object objVal = Util.readField(model, field);
// our object is not linked to another object...so it doesn't have any key
if (objVal == null) {
continue;
}
Key key = GaeMappingUtils.getKey(objVal);
linkedObj = linkedModels.get(key);
if (linkedObj == null) {
entity = entityMap.get(field).get(key);
linkedObj = objVal;
GaeMappingUtils.fillModel(linkedObj, entity);
linkedModels.put(key, linkedObj);
}
field.set(model, linkedObj);
}
}
return models;
} catch (IllegalAccessException ex) {
throw new SienaException(ex);
}
}
use of siena.SienaException in project siena by mandubian.
the class GaePersistenceManager method mapJoins.
protected <T> T mapJoins(T model) {
try {
// join queries
Map<Field, ArrayList<Key>> fieldMap = GaeQueryUtils.buildJoinFieldKeysMap(model);
// creates the list of joined entity keys to extract
for (Field field : fieldMap.keySet()) {
Key key = GaeMappingUtils.getKey(field.get(model));
List<Key> keys = fieldMap.get(field);
if (!keys.contains(key))
keys.add(key);
}
Map<Field, Map<Key, Entity>> entityMap = new HashMap<Field, Map<Key, Entity>>();
try {
// retrieves all joined entities per field
for (Field field : fieldMap.keySet()) {
Map<Key, Entity> entities = ds.get(fieldMap.get(field));
entityMap.put(field, entities);
}
} catch (Exception ex) {
throw new SienaException(ex);
}
// associates linked models to their models
// linkedModels is just a map to contain entities already mapped
Map<Key, Object> linkedModels = new HashMap<Key, Object>();
Object linkedObj;
Entity entity;
for (Field field : fieldMap.keySet()) {
Object objVal = field.get(model);
Key key = GaeMappingUtils.getKey(objVal);
linkedObj = linkedModels.get(key);
if (linkedObj == null) {
entity = entityMap.get(field).get(key);
linkedObj = objVal;
GaeMappingUtils.fillModel(linkedObj, entity);
linkedModels.put(key, linkedObj);
}
field.set(model, linkedObj);
}
return model;
} catch (IllegalAccessException ex) {
throw new SienaException(ex);
}
}
Aggregations