Revert "feat[orm]: id filed can use other name with @BsonId annotation"

This reverts commit e6544eaeb8.
This commit is contained in:
sando
2024-07-30 11:29:29 +08:00
parent e6544eaeb8
commit 50fb45dd66
3 changed files with 14 additions and 61 deletions
@@ -40,9 +40,7 @@ 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;
@@ -105,11 +103,7 @@ public class OrmManager implements IOrmManager {
allEntityCachesUsableMap.put(entityClass, false);
}
var pojoCodecProvider = PojoCodecProvider.builder()
.conventions(List.of(Conventions.ANNOTATION_CONVENTION))
.automatic(true)
.register(new MapCodecProvider())
.build();
var pojoCodecProvider = PojoCodecProvider.builder().automatic(true).register(new MapCodecProvider()).build();
var codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromProviders(pojoCodecProvider));
var mongoBuilder = MongoClientSettings
@@ -441,7 +435,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);
@@ -462,8 +456,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") && !idField.isAnnotationPresent(BsonId.class)) {
throw new RunException("use @BsonId annotation to @Id filed:[{}] or rename this field with id", idField.getName());
if (!idField.getName().equals("id")) {
throw new RunException("@Id filed must name with id");
}
ReflectionUtils.makeAccessible(idField);
@@ -1,41 +0,0 @@
/*
* 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);
}
}
@@ -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<String> {
@Id
@BsonId
private String mailId;
private String id;
@Index(ascending = true, unique = false)
private String userName;
@@ -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.id = id;
entity.userName = userName;
entity.content = content;
entity.createDate = createDate;
@@ -48,15 +48,15 @@ public class MailEntity implements IEntity<String> {
@Override
public String id() {
return mailId;
return id;
}
public String getMailId() {
return mailId;
public String getId() {
return id;
}
public void setMailId(String mailId) {
this.mailId = mailId;
public void setId(String id) {
this.id = id;
}
public String getUserName() {