Skip to content

Commit

Permalink
Merge pull request #128 from sandogeek/main
Browse files Browse the repository at this point in the history
feat[orm]: id filed can use other name with @BsonId or @id annotation
  • Loading branch information
jaysunxiao authored Jul 30, 2024
2 parents f809dbe + b2a45c7 commit 670100a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion orm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var collection = OrmContext.getOrmManager().getCollection(UserEntity.class)
#### 2. Indirect use (middle level API) to access data through the collection's simple wrapper IAccessor and IQuery interfaces

- IAccessor is a data access interface
- Inserting data into the database uses the return value of the object's id() method as the primary key
- Inserting data into the database uses the field value of the object's field which marked `@Id` as the primary key
```
OrmContext.getAccessor().insert(obj)
```
Expand Down
2 changes: 1 addition & 1 deletion orm/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var collection = OrmContext.getOrmManager().getCollection(UserEntity.class)
#### 2. 间接使用(middle level api),通过collection的简易包装类IAccessor和IQuery接口访问数据

- IAccessor接口,为数据访问接口
- 插入数据到数据库,会以对象的id()方法的返回值作为主键
- 插入数据到数据库,会以对象class的@Id字段的值作为主键
```
OrmContext.getAccessor().insert(obj)
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.zfoo.orm.convention;

import com.zfoo.orm.anno.Id;
import org.bson.BsonType;
import org.bson.codecs.pojo.ClassModelBuilder;
import org.bson.codecs.pojo.Convention;
import org.bson.codecs.pojo.PropertyModelBuilder;
import org.bson.codecs.pojo.annotations.*;

import java.lang.annotation.Annotation;

/**
* zfoo注解约定
*
* @author Sando
* @version 1.0
* @since 2024/7/30
*/
public class ZfooAnnotationConvention implements Convention {
public static final ZfooAnnotationConvention INSTANCE = new ZfooAnnotationConvention();
@Override
public void apply(ClassModelBuilder<?> classModelBuilder) {
for (PropertyModelBuilder<?> propertyModelBuilder : classModelBuilder.getPropertyModelBuilders()) {
processPropertyAnnotations(classModelBuilder, propertyModelBuilder);
}
}

private void processPropertyAnnotations(final ClassModelBuilder<?> classModelBuilder,
final PropertyModelBuilder<?> propertyModelBuilder) {
for (Annotation annotation : propertyModelBuilder.getReadAnnotations()) {
if (annotation instanceof Id) {
String idPropertyName = classModelBuilder.getIdPropertyName();
String fieldName = propertyModelBuilder.getName();
if (idPropertyName != null && !fieldName.equals(idPropertyName)) {
// allow using @Id and @BsonId on same field
String typeName = classModelBuilder.getType().getName();
throw new IllegalStateException("The class " +
typeName + " has more than one id property. The properties are " +
idPropertyName + " and " + fieldName);
}
classModelBuilder.idPropertyName(fieldName);
}
}
}
}
19 changes: 7 additions & 12 deletions orm/src/main/java/com/zfoo/orm/manager/OrmManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.zfoo.orm.config.CacheStrategy;
import com.zfoo.orm.config.OrmConfig;
import com.zfoo.orm.config.PersisterStrategy;
import com.zfoo.orm.convention.ZfooAnnotationConvention;
import com.zfoo.orm.model.EntityDef;
import com.zfoo.orm.model.IEntity;
import com.zfoo.orm.model.IndexDef;
Expand All @@ -42,7 +43,6 @@
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.pojo.Conventions;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -105,12 +105,11 @@ public void initBefore() {
allEntityCachesUsableMap.put(entityClass, false);
}

var pojoCodecProvider = PojoCodecProvider.builder()
.conventions(List.of(Conventions.ANNOTATION_CONVENTION))
.automatic(true)
.register(new MapCodecProvider())
.build();
var codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromProviders(pojoCodecProvider));
var pojoCodecProvider = PojoCodecProvider.builder().automatic(true)
.conventions(List.of(Conventions.ANNOTATION_CONVENTION, ZfooAnnotationConvention.INSTANCE))
.register(new MapCodecProvider()).build();
var codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
CodecRegistries.fromProviders(pojoCodecProvider));

var mongoBuilder = MongoClientSettings
.builder()
Expand Down Expand Up @@ -441,7 +440,7 @@ private void checkIdField(Class<?> clazz) {
var getIdMethod = ReflectionUtils.getMethodByNameInPOJOClass(clazz, FieldUtils.fieldToGetMethod(clazz, idField));
var returnTypeOfGetIdMethod = getIdMethod.getReturnType();
AssertionUtils.isTrue(returnTypeOfGetIdMethod.equals(idFieldType), "[{}] getIdMethod:[{}] return type:[{}] must be equal with type id:[{}]"
, clazz.getSimpleName(), getIdMethod.getName(), returnTypeOfGetIdMethod.getName(), idFieldType.getName());
, clazz.getSimpleName(), getIdMethod.getName(),returnTypeOfGetIdMethod.getName(), idFieldType.getName());

// 随机给id字段赋值,然后调用id()方法,看看两者的返回值是不是一样的,避免出错
var entityInstance = ReflectionUtils.newInstance(clazz);
Expand All @@ -462,10 +461,6 @@ private void checkIdField(Class<?> clazz) {
throw new RunException("orm id field only supports int long float double String ObjectId");
}

if (!idField.getName().equals("id") && !idField.isAnnotationPresent(BsonId.class)) {
throw new RunException("use @BsonId annotation to @Id filed:[{}] or rename this field with id", idField.getName());
}

ReflectionUtils.makeAccessible(idField);
ReflectionUtils.setField(idField, entityInstance, idFiledValue);
var idMethodOptional = Arrays.stream(ReflectionUtils.getAllMethods(clazz)).filter(it -> it.getName().equalsIgnoreCase("id"))
Expand Down
8 changes: 4 additions & 4 deletions orm/src/test/java/com/zfoo/orm/entity/MailEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@

package com.zfoo.orm.entity;

import com.zfoo.orm.anno.EntityCache;
import com.zfoo.orm.anno.Id;
import com.zfoo.orm.anno.Index;
import com.zfoo.orm.model.IEntity;
import org.bson.codecs.pojo.annotations.BsonId;

import java.util.Date;


/**
* @author godotg
*/
@EntityCache
public class MailEntity implements IEntity<String> {

@Id
@BsonId
private String mailId;

@Index(ascending = true, unique = false)
Expand All @@ -37,9 +37,9 @@ public class MailEntity implements IEntity<String> {

private Date createDate;

public static MailEntity valueOf(String mailId, String userName, String content, Date createDate) {
public static MailEntity valueOf(String id, String userName, String content, Date createDate) {
var entity = new MailEntity();
entity.mailId = mailId;
entity.mailId = id;
entity.userName = userName;
entity.content = content;
entity.createDate = createDate;
Expand Down

0 comments on commit 670100a

Please # to comment.