首先,我想说是 - 我知道有像Morphia和Spring Data for MongoDB这样的ORM。我不是想重新发明 - 只是为了学习。因此,我的AbstractRepository背后的基本思想是封装在所有存储库之间共享的逻辑。子类(特定实体的存储库)将Entity类传递给。
使用Reflection将实体bean(POJO)转换为DBObject非常简单。将DBObject转换为实体bean会出现问题。原因?我需要将DBObject中的任何字段类型转换为实体bean属性类型。这就是我被困的地方。我无法在AbstractRepository方法T getEntityFromDBObject(DBObject object)
我可以将实体类传递给这个方法,但这会破坏多态的目的。另一种方法是声明private T type
属性,然后使用Field读取类型。定义附加属性只是为了让我能读得不对。
所以问题是 - 如何使用反射使用较少的参数将DBObject映射到POJO。这又是一个想法:
public abstract class AbstractRepository<T> {
T getEntityFromDBObject(DBObject object) {
....
}
}
具体的存储库看起来像这样:
public class EntityRepository extends AbstractRepository<T> {
}
谢谢!
注意:忽略复杂的关系和引用。让我们说它不需要支持对另一个DBObject或POJO的引用。
答案 0 :(得分:1)
你需要构建一个T类型的实例并用'DBObject'中的数据填充它:
public abstract class AbstractRepository<T> {
protected final Class<T> entityClass;
protected AbstractRepository() {
// Don't remember if this reflection stuff throws any exception
// If it does, try-catch and throw RuntimeException
// (or assign null to entityClass)
// Anyways, it's impossible that such exception occurs here
Type t = this.getClass().getGenericSuperclass();
this.entityClass = ((Class<T>)((ParameterizedType)t).getActualTypeArguments()[0]);
}
T getEntityFromDBObject(DBObject object) {
// Use reflection to create an entity instance
// Let's suppose all entities have a public no-args constructor (they should!)
T entity = (T) this.entityClass.getConstructor().newInstance();
// Now fill entity with DBObject's data
// This is the place to fill common fields only, i.e. an ID
// So maybe T could extend some abstract BaseEntity that provides setters for these common fields
// Again, all this reflection stuff needs to be done within a try-catch block because of checked exceptions
// Wrap the original exception in a RuntimeException and throw this one instead
// (or maybe your own specific runtime exception for this case)
// Now let specialized repositories fill specific fields
this.fillSpecificFields(entity, object);
return entity;
}
protected abstract void fillSpecificFields(T entity, DBObject object);
}
如果您不想在每个实体的存储库中实现方法.fillSpecificFields()
,那么您需要使用反射来设置每个字段(包括ID等常用字段,因此不要手动设置它们) )。
如果是这种情况,您已将实体类作为受保护属性,因此它可供每个实体的存储库使用。您需要遍历 ALL 其字段,包括在超类中声明的字段(我相信您必须使用方法.getFields()
而不是.getDeclaredFields()
)并通过反射设置值。
作为旁注,我真的不知道DBObject
实例中的数据是什么,以及采用何种格式,所以如果从中提取字段的值是非常重要的,请告诉我。< / p>
答案 1 :(得分:1)
首先,我想在近两个月后回答您的意见。我确实设法自己弄明白了,这就是我如何实现它(并经过测试)所以也许有人会使用它:
public abstract class AbstractRepository<T> {
@Inject
private MongoConnectionProvider provider;
// Keeps current repository collection name
protected String collectionName;
@PostConstruct
public abstract void initialize();
public String getCollectionName() {
return this.collectionName;
}
protected void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
protected DBCollection getConnection() {
DB conn = this.provider.getConnection();
DBCollection collection = conn.getCollection(this.collectionName);
return collection;
}
private void putFieldToDbObject(T source, DBObject target, Field field) {
// TODO: Think more elegant solution for this
try {
field.setAccessible(true);
// Should we cast String to ObjectId
if (field.getName() == "id" && field.get(source) != null
|| field.isAnnotationPresent(DBRef.class)) {
String key = field.getName().equals("id") ? "_id" : field.getName();
target.put(key, new ObjectId(field.get(source).toString()));
} else {
if(!field.getName().equals("id")) {
target.put(field.getName(), field.get(source));
}
}
} catch (IllegalArgumentException | IllegalAccessException exception) {
// TODO Auto-generated catch block
exception.printStackTrace();
} finally {
field.setAccessible(false);
}
}
@SuppressWarnings("rawtypes")
protected DBObject getDbObject(T entity) {
DBObject result = new BasicDBObject();
// Get entity class
Class entityClass = entity.getClass();
Field[] fields = entityClass.getDeclaredFields();
// Update DBobject with entity data
for (Field field : fields) {
this.putFieldToDbObject(entity, result, field);
}
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public T getEntityFromDBObject(DBObject object) throws MappingException {
Type superclass = this.getClass().getGenericSuperclass();
Type entityClass = ((ParameterizedType) superclass).getActualTypeArguments()[0];
T entity;
try {
entity = ((Class<T>) entityClass).newInstance();
// Map fields to entity properties
Set<String> keySet = object.keySet();
for(String key : keySet) {
String fieldName = key.equals("_id") ? "id" : key;
Field field = ((Class<T>) entityClass).getDeclaredField(fieldName);
field.setAccessible(true);
if(object.get(key).getClass().getSimpleName().equals("ObjectId")) {
field.set(entity, object.get(key).toString());
} else {
// Get field type
Type fieldType = field.getType();
Object fieldValue = object.get(key);
Class objectType = object.get(key).getClass();
if(!fieldType.equals(objectType)) {
// Let's try to convert source type to destination type
try {
fieldValue = (((Class) fieldType).getConstructor(objectType)).newInstance(object.get(key));
} catch (NoSuchMethodException exception) {
// Let's try to use String as "man-in-the-middle"
String objectValue = object.get(key).toString();
// Get constructor for destination type that take String as parameter
Constructor constructor = ((Class) fieldType).getConstructor(String.class);
fieldValue = constructor.newInstance(objectValue);
}
}
field.set(entity, fieldValue);
}
field.setAccessible(false);
}
} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException e) {
throw new MappingException(e.getMessage(), MappingExceptionCode.UNKNOWN_ERROR);
}
return entity;
}
public List<T> getAll() {
DBCollection conn = this.getConnection();
DBCursor cursor = conn.find();
List<T> result = new LinkedList<T>();
while (cursor.hasNext()) {
DBObject obj = cursor.next();
try {
result.add(this.getEntityFromDBObject(obj));
} catch (MappingException e) {
}
}
return result;
}
public T getOneById(String id) {
DBObject idRef = new BasicDBObject().append("_id", new ObjectId(id));
DBCollection conn = this.getConnection();
DBObject resultObj = conn.findOne(idRef);
T result = null;
try {
result = this.getEntityFromDBObject(resultObj);
} catch (MappingException e) {
}
return result;
}
public void save(T entity) {
DBObject object = this.getDbObject(entity);
DBCollection collection = this.getConnection();
collection.save(object);
}
}
答案 2 :(得分:0)
你偶然发现了对象映射的问题。有一些图书馆可以帮助解决这个问题。您可以查看ModelMapper(此处作者)。