本文整理汇总了Java中org.mongodb.morphia.annotations.Reference类的典型用法代码示例。如果您正苦于以下问题:Java Reference类的具体用法?Java Reference怎么用?Java Reference使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Reference类属于org.mongodb.morphia.annotations包,在下文中一共展示了Reference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: check
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
@Override
protected void check(final Mapper mapper, final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
if (mf.hasAnnotation(Reference.class)) {
final Class realType = (mf.isSingleValue()) ? mf.getType() : mf.getSubClass();
if (realType == null) {
throw new MappingException("Type is null for this MappedField: " + mf);
}
if ((!realType.isInterface() && mapper.getMappedClass(realType).getIdField() == null)) {
ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
mf.getFullName() + " is annotated as a @" + Reference.class.getSimpleName() + " but the "
+ mf.getType().getName()
+ " class is missing the @" + Id.class.getSimpleName() + " annotation"));
}
}
}
示例2: toDBObject
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
@Override
public void toDBObject(final Object entity, final MappedField mf, final DBObject dbObject, final Map<Object, DBObject> involvedObjects,
final Mapper mapper) {
final String name = mf.getNameToStore();
final Object fieldValue = mf.getFieldValue(entity);
if (fieldValue == null && !mapper.getOptions().isStoreNulls()) {
return;
}
final Reference refAnn = mf.getAnnotation(Reference.class);
if (mf.isMap()) {
writeMap(mf, dbObject, name, fieldValue, refAnn, mapper);
} else if (mf.isMultipleValues()) {
writeCollection(mf, dbObject, name, fieldValue, refAnn, mapper);
} else {
writeSingle(dbObject, name, fieldValue, refAnn, mapper);
}
}
示例3: readSingle
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
private void readSingle(final Datastore datastore, final Mapper mapper, final Object entity, final Class fieldType,
final Reference annotation, final EntityCache cache, final MappedField mf, final DBObject dbObject) {
final Object ref = mf.getDbObjectValue(dbObject);
if (ref != null) {
Object resolvedObject;
if (annotation.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) {
resolvedObject = createOrReuseProxy(datastore, mapper, fieldType, ref, cache, annotation);
} else {
resolvedObject = resolveObject(datastore, mapper, cache, mf, annotation.idOnly(), ref);
}
if (resolvedObject != null) {
mf.setFieldValue(entity, resolvedObject);
}
}
}
示例4: writeSingle
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
private void writeSingle(final DBObject dbObject, final String name, final Object fieldValue, final Reference refAnn,
final Mapper mapper) {
if (fieldValue == null) {
if (mapper.getOptions().isStoreNulls()) {
dbObject.put(name, null);
}
} else {
Key<?> key = getKey(fieldValue, mapper);
if (refAnn.idOnly()) {
Object id = mapper.keyToId(key);
if (id != null && mapper.isMapped(id.getClass())) {
id = mapper.toMongoObject(id, true);
}
dbObject.put(name, id);
} else {
dbObject.put(name, mapper.keyToDBRef(key));
}
}
}
示例5: writeMappedField
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
private void writeMappedField(final DBObject dbObject, final MappedField mf, final Object entity,
final Map<Object, DBObject> involvedObjects) {
//skip not saved fields.
if (mf.hasAnnotation(NotSaved.class)) {
return;
}
// get the annotation from the field.
Class<? extends Annotation> annType = getFieldAnnotation(mf);
if (Property.class.equals(annType) || Serialized.class.equals(annType) || mf.isTypeMongoCompatible()
|| (getConverters().hasSimpleValueConverter(mf) || (getConverters().hasSimpleValueConverter(mf.getFieldValue(entity))))) {
opts.getValueMapper().toDBObject(entity, mf, dbObject, involvedObjects, this);
} else if (Reference.class.equals(annType)) {
opts.getReferenceMapper().toDBObject(entity, mf, dbObject, involvedObjects, this);
} else if (Embedded.class.equals(annType)) {
opts.getEmbeddedMapper().toDBObject(entity, mf, dbObject, involvedObjects, this);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("No annotation was found, using default mapper " + opts.getDefaultMapper() + " for " + mf);
}
opts.getDefaultMapper().toDBObject(entity, mf, dbObject, involvedObjects, this);
}
}
示例6: addCondition
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
public void addCondition(ArrayList<ArrayList<Criteria>> criteria, Object value, Class<?> clazz) {
if(!(value == null && nullOption == NullOption.IGNORE_WHEN_NULL)){
Criteria newCriteria;
boolean composite = false;
if(propertyName.indexOf(".") != -1){
for(Field field : clazz.getDeclaredFields()){
if(field.getName().equalsIgnoreCase(propertyName.substring(0, propertyName.indexOf(".")))){
if(field.isAnnotationPresent(Reference.class))
composite = true;
}
}
}
if(value != null){
if(composite){
newCriteria = compositeProperty(clazz, value);
}
else{
switch(compType){
case GREATER_OR_EQUALS: newCriteria = getQuery(clazz).criteria(propertyName).greaterThanOrEq(value); break;
case LESSER_OR_EQUALS: newCriteria = getQuery(clazz).criteria(propertyName).lessThanOrEq(value); break;
case GREATER: newCriteria = getQuery(clazz).criteria(propertyName).greaterThan(value); break;
case LESSER: newCriteria = getQuery(clazz).criteria(propertyName).lessThan(value); break;
case NOT_EQUALS: newCriteria = getQuery(clazz).criteria(propertyName).notEqual(value); break;
case EQUALS: newCriteria = getQuery(clazz).criteria(propertyName).equal(value); break;
default: newCriteria = getQuery(clazz).criteria(propertyName).contains((String)value); break;
}
}
}
else{
if(composite)
newCriteria = getQuery(clazz).criteria(propertyName.substring(0, propertyName.indexOf("."))).equal(null);
else
newCriteria = getQuery(clazz).criteria(propertyName).equal(null);
}
criteria.get(criteria.size() - 1).add(newCriteria);
if(nextConector.equals("and")){
//Do nothing
}else if(nextConector.equals("or")){
criteria.add(new ArrayList<Criteria>());
}else if(nextConector.equals("")){
//Do nothing
}else{
System.err.println("Unsupported connector!");
}
}
}
示例7: getConstraints
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
private List<ClassConstraint> getConstraints() {
final List<ClassConstraint> constraints = new ArrayList<ClassConstraint>(32);
// normally, i do this with scanning the classpath, but that´d bring
// another dependency ;)
// class-level
constraints.add(new MultipleId());
constraints.add(new MultipleVersions());
constraints.add(new NoId());
constraints.add(new EmbeddedAndId());
constraints.add(new EntityAndEmbed());
constraints.add(new EmbeddedAndValue());
constraints.add(new EntityCannotBeMapOrIterable());
constraints.add(new DuplicatedAttributeNames());
// constraints.add(new ContainsEmbeddedWithId());
// field-level
constraints.add(new MisplacedProperty());
constraints.add(new ReferenceToUnidentifiable());
constraints.add(new LazyReferenceMissingDependencies());
constraints.add(new LazyReferenceOnArray());
constraints.add(new MapKeyDifferentFromString());
constraints.add(new MapNotSerializable());
constraints.add(new VersionMisuse(creator));
//
constraints.add(new ContradictingFieldAnnotation(Reference.class, Serialized.class));
constraints.add(new ContradictingFieldAnnotation(Reference.class, Property.class));
constraints.add(new ContradictingFieldAnnotation(Reference.class, Embedded.class));
//
constraints.add(new ContradictingFieldAnnotation(Embedded.class, Serialized.class));
constraints.add(new ContradictingFieldAnnotation(Embedded.class, Property.class));
//
constraints.add(new ContradictingFieldAnnotation(Property.class, Serialized.class));
return constraints;
}
示例8: check
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
@Override
protected void check(final Mapper mapper, final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
// an @Id field can not be a Value, Reference, or Embedded
if (mf.hasAnnotation(Id.class)) {
if (mf.hasAnnotation(Reference.class) || mf.hasAnnotation(Embedded.class) || mf.hasAnnotation(Property.class)) {
ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
mf.getFullName() + " is annotated as @" + Id.class.getSimpleName()
+ " and cannot be mixed with other annotations (like @Reference)"));
}
}
}
示例9: check
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
@Override
protected void check(final Mapper mapper, final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
final Reference ref = mf.getAnnotation(Reference.class);
if (ref != null && ref.lazy()) {
final Class type = mf.getType();
if (type.isArray()) {
ve.add(new ConstraintViolation(Level.FATAL, mc, mf, getClass(),
"The lazy attribute cannot be used for an Array. If you need a lazy array "
+ "please use ArrayList instead."));
}
}
}
示例10: check
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
@Override
protected void check(final Mapper mapper, final MappedClass mc, final MappedField mf, final Set<ConstraintViolation> ve) {
final Reference ref = mf.getAnnotation(Reference.class);
if (ref != null) {
if (ref.lazy()) {
if (!LazyFeatureDependencies.testDependencyFullFilled()) {
ve.add(new ConstraintViolation(Level.SEVERE, mc, mf, getClass(),
"Lazy references need CGLib and Proxytoys in the classpath."));
}
}
}
}
示例11: fromDBObject
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
@Override
public void fromDBObject(final Datastore datastore, final DBObject dbObject, final MappedField mf, final Object entity,
final EntityCache cache, final Mapper mapper) {
final Class fieldType = mf.getType();
final Reference refAnn = mf.getAnnotation(Reference.class);
if (mf.isMap()) {
readMap(datastore, mapper, entity, refAnn, cache, mf, dbObject);
} else if (mf.isMultipleValues()) {
readCollection(datastore, mapper, dbObject, mf, entity, refAnn, cache);
} else {
readSingle(datastore, mapper, entity, fieldType, refAnn, cache, mf, dbObject);
}
}
示例12: createOrReuseProxy
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
private Object createOrReuseProxy(final Datastore datastore, final Mapper mapper, final Class referenceObjClass, final Object ref,
final EntityCache cache, final Reference anntotation) {
final Key key = anntotation.idOnly() ? mapper.manualRefToKey(referenceObjClass, ref) : mapper.refToKey((DBRef) ref);
final Object proxyAlreadyCreated = cache.getProxy(key);
if (proxyAlreadyCreated != null) {
return proxyAlreadyCreated;
}
final Object newProxy = mapper.getProxyFactory().createProxy(datastore, referenceObjClass, key, anntotation.ignoreMissing());
cache.putProxy(key, newProxy);
return newProxy;
}
示例13: readMap
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
private void readMap(final Datastore datastore, final Mapper mapper, final Object entity, final Reference refAnn,
final EntityCache cache, final MappedField mf, final DBObject dbObject) {
final Class referenceObjClass = mf.getSubClass();
Map m = mapper.getOptions().getObjectFactory().createMap(mf);
final DBObject dbVal = (DBObject) mf.getDbObjectValue(dbObject);
if (dbVal != null) {
if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) {
// replace map by proxy to it.
m = mapper.getProxyFactory().createMapProxy(datastore, m, referenceObjClass, refAnn.ignoreMissing());
}
final Map map = m;
new IterHelper<Object, Object>().loopMap(dbVal, new MapIterCallback<Object, Object>() {
@Override
public void eval(final Object k, final Object val) {
final Object objKey = mapper.getConverters().decode(mf.getMapKeyClass(), k, mf);
if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) {
final ProxiedEntityReferenceMap proxiedMap = (ProxiedEntityReferenceMap) map;
proxiedMap.__put(objKey, refAnn.idOnly()
? mapper.manualRefToKey(referenceObjClass, val)
: mapper.refToKey((DBRef) val));
} else {
map.put(objKey, resolveObject(datastore, mapper, cache, mf, refAnn.idOnly(), val));
}
}
});
}
mf.setFieldValue(entity, m);
}
示例14: getDBRefs
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
private Object getDBRefs(final MappedField field, final Iterable value) {
final List<Object> refs = new ArrayList<Object>();
Reference annotation = field.getAnnotation(Reference.class);
boolean idOnly = annotation != null && annotation.idOnly();
for (final Object o : value) {
Key<?> key = (o instanceof Key) ? (Key<?>) o : getKey(o);
refs.add(idOnly ? key.getId() : keyToDBRef(key));
}
return refs;
}
示例15: getFieldAnnotation
import org.mongodb.morphia.annotations.Reference; //导入依赖的package包/类
private Class<? extends Annotation> getFieldAnnotation(final MappedField mf) {
Class<? extends Annotation> annType = null;
for (final Class<? extends Annotation> testType : new Class[]{Property.class, Embedded.class, Serialized.class, Reference.class}) {
if (mf.hasAnnotation(testType)) {
annType = testType;
break;
}
}
return annType;
}