@Override public void prePersist(final Object entity, final DBObject dbObj, final Mapper mapper) { MappedClass mclass = mapper.getMappedClass(entity); Field id = mclass.getIdField(); if (id != null && id.getAnnotation(GeneratedValue.class) != null) { try { id.setAccessible(true); final String collName = gen.value(mclass.getClazz()); final Query<StoredId> q = db.find(StoredId.class, "_id", collName); final UpdateOperations<StoredId> uOps = db.createUpdateOperations(StoredId.class) .inc("value"); StoredId newId = db.findAndModify(q, uOps); if (newId == null) { newId = new StoredId(collName); db.save(newId); } id.set(entity, newId.value); } catch (Exception ex) { throw new IllegalStateException("Can't generate ID on " + mclass, ex); } } }
public <T> T findAndModify(Query<T> query, UpdateOperations<T> operations, FindAndModifyOptions options) { return datastore.findAndModify(query, operations, options); }
public <T> T findAndModify(Query<T> query, UpdateOperations<T> operations) { return datastore.findAndModify(query, operations); }
public <T> T findAndModify(MorphiaQuery q, boolean oldVersion) { return (T)ds().findAndModify((Query)q.getMorphiaQuery(), (UpdateOperations)u_, oldVersion); }
public <T> T findAndModify(MorphiaQuery q, boolean oldVersion, boolean createIfMissing) { return (T)ds().findAndModify((Query)q.getMorphiaQuery(), (UpdateOperations)u_, oldVersion, createIfMissing); }
public <T> T findAndModify(MorphiaQuery q) { return (T)ds().findAndModify((Query)q.getMorphiaQuery(), (UpdateOperations)u_); }
@Override public void prePersist(final Object entity, final DBObject dbObj, final Mapper mapper) { MappedClass mclass = mapper.getMappedClass(entity); Field id = mclass.getIdField(); if (id != null && id.getAnnotation(GeneratedValue.class) != null) { try { id.setAccessible(true); final String collName = gen.value(mclass.getClazz()); final Query<StoredId> q = db.find(StoredId.class, "_id", collName); final UpdateOperations<StoredId> uOps = db.createUpdateOperations(StoredId.class) .inc("value"); StoredId newId = db.findAndModify(q, uOps); if (newId == null) { newId = new StoredId(collName); db.save(newId); } id.set(entity, newId.value); } catch (Exception ex) { throw new IllegalStateException("Can't generate ID on " + mclass, ex); } } }
/** * Returns the next number in the sequence specified . If sequence does not exists * then it will be created * @param name the sequence name * @return the next number in the sequence */ public long next(String name) { UpdateOperations<Sequence> op = ds.createUpdateOperations(Sequence.class); op.inc("number"); Query<Sequence> q = ds.createQuery(Sequence.class).field("_id").equal(name); Sequence seq = ds.findAndModify(q, op, false, true); return seq.number; }
public static Seq next(String name) { Datastore ds = MorphiaPlugin.ds(); Query<Seq> q = ds.find(Seq.class, "_id", name); UpdateOperations<Seq> o = ds.createUpdateOperations(Seq.class).inc("value"); Seq newId = ds.findAndModify(q, o); if (null == newId) { newId = new Seq(name); ds.save(newId); } return newId; }
public int assignNextBuildNumber(final DynamicProject project) { final Datastore datastore = getDatastore(); BuildNumberCounter seq = datastore.findAndModify( datastore.find(BuildNumberCounter.class, "key = ", project.getFullName()), // query datastore.createUpdateOperations(BuildNumberCounter.class).inc("counter") // update ); if (seq == null) { seq = new BuildNumberCounter(project.getFullName(), 1); datastore.save(seq); } return seq.getCounter(); }