LongIdEntity.java :  » MongoDB » morphia » com » google » code » morphia » utils » Java Open Source

Java Open Source » MongoDB » morphia 
morphia » com » google » code » morphia » utils » LongIdEntity.java
package com.google.code.morphia.utils;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.PrePersist;
import com.google.code.morphia.annotations.Transient;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;

public abstract class LongIdEntity {
  @Id protected Long myLongId;
  
  @Transient final protected Datastore ds;
  
  protected LongIdEntity(Datastore ds) {
    this.ds = ds;
  }
  
  @PrePersist void prePersist(){
    if (myLongId == null) {
      String collName = ds.getCollection(getClass()).getName();
        Query<StoredId> q = ds.find(StoredId.class, "_id", collName);
        UpdateOperations<StoredId> uOps = ds.createUpdateOperations(StoredId.class).inc("value");
        StoredId newId = ds.findAndModify(q, uOps);
        if (newId == null) {
           newId = new StoredId(collName);
           ds.save(newId);
        }
        
        myLongId = newId.getValue();
    }
  }
  /**
   * Used to store counters for other entities.
   * @author skot
   *
   */

  @Entity(value="ids", noClassnameStored=true)
  public static class StoredId {
    final @Id String className;
    protected Long value = 1L;
    
    public StoredId(String name) {
      className = name;
    }
    
    protected StoredId(){
      className = "";
    }
    
    public Long getValue() {
      return value;
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.