use of siena.Id in project siena by mandubian.
the class JdbcPersistenceManager method save.
public void save(Object obj) {
JdbcClassInfo classInfo = JdbcClassInfo.getClassInfo(obj.getClass());
PreparedStatement ps = null;
try {
Field idField = classInfo.info.getIdField();
Object idVal = Util.readField(obj, idField);
if (idVal == null) {
for (Field field : classInfo.keys) {
Id id = field.getAnnotation(Id.class);
if (id.value() == Generator.UUID) {
field.set(obj, UUID.randomUUID().toString());
}
}
}
if (idVal == null && !classInfo.generatedKeys.isEmpty()) {
ps = getConnection().prepareStatement(classInfo.insertOrUpdateSQL, Statement.RETURN_GENERATED_KEYS);
// insertWithAutoIncrementKey(classInfo, obj);
} else {
ps = getConnection().prepareStatement(classInfo.insertOrUpdateSQL);
}
int i = 1;
i = addParameters(obj, classInfo.allFields, ps, i);
addParameters(obj, classInfo.updateFields, ps, i);
ps.executeUpdate();
if (idVal == null && !classInfo.generatedKeys.isEmpty()) {
ResultSet gk = ps.getGeneratedKeys();
if (!gk.next())
throw new SienaException("No such generated keys");
i = 1;
for (Field field : classInfo.generatedKeys) {
field.setAccessible(true);
JdbcMappingUtils.setFromObject(obj, field, gk.getObject(i));
// field.set(obj, gk.getObject(i));
i++;
}
}
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
} finally {
JdbcDBUtils.closeStatementAndConnection(this, ps);
}
}
use of siena.Id in project siena by mandubian.
the class JdbcPersistenceManager method insert.
public int insert(Iterable<?> objects) {
Map<JdbcClassInfo, List<Object>> objMap = new HashMap<JdbcClassInfo, List<Object>>();
PreparedStatement ps = null;
for (Object obj : objects) {
JdbcClassInfo classInfo = JdbcClassInfo.getClassInfo(obj.getClass());
if (!objMap.containsKey(classInfo)) {
List<Object> l = new ArrayList<Object>();
l.add(obj);
objMap.put(classInfo, l);
} else {
objMap.get(classInfo).add(obj);
}
}
int total = 0;
try {
for (JdbcClassInfo classInfo : objMap.keySet()) {
if (classInfo.generatedKeys.isEmpty()) {
ps = getConnection().prepareStatement(classInfo.insertSQL);
for (Object obj : objMap.get(classInfo)) {
for (Field field : classInfo.keys) {
Id id = field.getAnnotation(Id.class);
if (id.value() == Generator.UUID) {
field.set(obj, UUID.randomUUID().toString());
}
}
// TODO: implement primary key generation: SEQUENCE
addParameters(obj, classInfo.insertFields, ps, 1);
ps.addBatch();
}
// TODO what to do with results of executeBatch ??????
int[] res = ps.executeBatch();
total += res.length;
} else {
total += insertBatchWithAutoIncrementKey(classInfo, objMap);
}
}
return total;
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
} finally {
JdbcDBUtils.closeStatementAndConnection(this, ps);
}
}
use of siena.Id in project siena by mandubian.
the class PostgresqlPersistenceManager method insertBatchWithAutoIncrementKey.
/**
* required to be overriden for Postgres
*
* @param classInfo
* @param objMap
* @throws SQLException
* @throws IllegalAccessException
*/
@Override
protected int insertBatchWithAutoIncrementKey(JdbcClassInfo classInfo, Map<JdbcClassInfo, List<Object>> objMap) throws SQLException, IllegalAccessException {
List<String> keyNames = new ArrayList<String>();
for (Field field : classInfo.generatedKeys) {
keyNames.add(field.getName());
}
// can't use batch in Postgres with generated keys... known bug
// http://postgresql.1045698.n5.nabble.com/PreparedStatement-batch-statement-impossible-td3406927.html
PreparedStatement ps = null;
ResultSet gk = null;
int res = 0;
try {
ps = getConnection().prepareStatement(classInfo.insertSQL + " RETURNING " + Util.join(keyNames, ","));
for (Object obj : objMap.get(classInfo)) {
for (Field field : classInfo.keys) {
Id id = field.getAnnotation(Id.class);
if (id.value() == Generator.UUID) {
field.set(obj, UUID.randomUUID().toString());
}
}
// TODO: implement primary key generation: SEQUENCE
addParameters(obj, classInfo.insertFields, ps, 1);
gk = ps.executeQuery();
if (!gk.next())
throw new SienaException("No such generated keys");
int i = 1;
for (Field field : classInfo.generatedKeys) {
// field.setAccessible(true);
Util.setFromObject(obj, field, gk.getObject(i));
// field.set(obj, gk.getObject(i));
i++;
}
JdbcDBUtils.closeResultSet(gk);
res++;
}
} finally {
JdbcDBUtils.closeStatementAndConnection(this, ps);
}
return res;
}
use of siena.Id in project siena by mandubian.
the class SdbMappingUtils method getItemNameFromKey.
public static String getItemNameFromKey(Class<?> clazz, Object key) {
Field idField = ClassInfo.getIdField(clazz);
Id id = idField.getAnnotation(Id.class);
String keyVal = null;
if (id != null) {
switch(id.value()) {
case NONE:
{
keyVal = toString(idField, key);
break;
}
case AUTO_INCREMENT:
// manages String ID as not long!!!
throw new SienaRestrictedApiException("DB", "getItemName", "@Id AUTO_INCREMENT not supported by SDB");
case UUID:
{
keyVal = toString(idField, key);
break;
}
default:
throw new SienaRestrictedApiException("DB", "createEntityInstance", "Id Generator " + id.value() + " not supported");
}
} else
throw new SienaException("Field " + idField.getName() + " is not an @Id field");
return keyVal;
}
use of siena.Id in project siena by mandubian.
the class SdbMappingUtils method getItemName.
public static String getItemName(Class<?> clazz, Object obj) {
Field idField = ClassInfo.getIdField(clazz);
Id id = idField.getAnnotation(Id.class);
String keyVal = null;
if (id != null) {
switch(id.value()) {
case NONE:
{
Object idVal = Util.readField(obj, idField);
if (idVal == null)
throw new SienaException("Id Field " + idField.getName() + " value null");
keyVal = toString(idField, idVal);
break;
}
case AUTO_INCREMENT:
// manages String ID as not long!!!
throw new SienaRestrictedApiException("DB", "getItemName", "@Id AUTO_INCREMENT not supported by SDB");
case UUID:
{
Object idVal = Util.readField(obj, idField);
if (idVal == null) {
UUID uuid = UUID.randomUUID();
keyVal = uuid.toString();
if (idField.getType() == UUID.class) {
Util.setField(obj, idField, uuid);
} else if (idField.getType() == String.class) {
Util.setField(obj, idField, uuid.toString());
} else {
throw new SienaRestrictedApiException("DB", "getItemName", "@Id UUID must be of type String or UUID");
}
} else {
keyVal = toString(idField, idVal);
}
break;
}
default:
throw new SienaRestrictedApiException("DB", "createEntityInstance", "Id Generator " + id.value() + " not supported");
}
} else
throw new SienaException("Field " + idField.getName() + " is not an @Id field");
return keyVal;
}
Aggregations