diff --git a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java index 41d44096..d4788d00 100644 --- a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java +++ b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java @@ -40,7 +40,9 @@ import com.zfoo.protocol.exception.RunException; import com.zfoo.protocol.util.*; import org.bson.Document; 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; @@ -103,7 +105,11 @@ public class OrmManager implements IOrmManager { allEntityCachesUsableMap.put(entityClass, false); } - var pojoCodecProvider = PojoCodecProvider.builder().automatic(true).register(new MapCodecProvider()).build(); + 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 mongoBuilder = MongoClientSettings @@ -435,7 +441,7 @@ public class OrmManager implements IOrmManager { 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); @@ -456,8 +462,8 @@ public class OrmManager implements IOrmManager { throw new RunException("orm id field only supports int long float double String ObjectId"); } - if (!idField.getName().equals("id")) { - throw new RunException("@Id filed must name with id"); + 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); diff --git a/orm/src/test/java/com/zfoo/orm/accessor/IdAnnotationTest.java b/orm/src/test/java/com/zfoo/orm/accessor/IdAnnotationTest.java new file mode 100644 index 00000000..9d1e16e9 --- /dev/null +++ b/orm/src/test/java/com/zfoo/orm/accessor/IdAnnotationTest.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.orm.accessor; + +import com.zfoo.orm.OrmContext; +import com.zfoo.orm.entity.MailEntity; +import com.zfoo.protocol.util.StringUtils; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.Date; + +@Ignore +public class IdAnnotationTest { + + @Test + public void batchInsertTest() { + var context = new ClassPathXmlApplicationContext("application.xml"); + for (int i = 0; i < 100; i++) { + insertMail(StringUtils.format("mail-{}", i)); + } + } + + public void insertMail(String mailId) { + OrmContext.getAccessor().delete(mailId, MailEntity.class); + var mailEntity = MailEntity.valueOf(mailId, "userName-" + mailId, "content" + mailId, new Date()); + OrmContext.getAccessor().insert(mailEntity); + } +} diff --git a/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java b/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java index e5a69e05..7e91db53 100644 --- a/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java +++ b/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java @@ -13,10 +13,10 @@ 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; @@ -24,11 +24,11 @@ import java.util.Date; /** * @author godotg */ -@EntityCache public class MailEntity implements IEntity { @Id - private String id; + @BsonId + private String mailId; @Index(ascending = true, unique = false) private String userName; @@ -37,9 +37,9 @@ public class MailEntity implements IEntity { private Date createDate; - public static MailEntity valueOf(String id, String userName, String content, Date createDate) { + public static MailEntity valueOf(String mailId, String userName, String content, Date createDate) { var entity = new MailEntity(); - entity.id = id; + entity.mailId = mailId; entity.userName = userName; entity.content = content; entity.createDate = createDate; @@ -48,15 +48,15 @@ public class MailEntity implements IEntity { @Override public String id() { - return id; + return mailId; } - public String getId() { - return id; + public String getMailId() { + return mailId; } - public void setId(String id) { - this.id = id; + public void setMailId(String mailId) { + this.mailId = mailId; } public String getUserName() {