mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-18 02:15:09 +00:00
Revert "feat[orm]: id filed can use other name with @BsonId annotation"
This reverts commit e6544eaeb8.
This commit is contained in:
1 parent
e6544eaeb8
commit
50fb45dd66
3 files changed
+14
-61
No files matched your search
@@ -40,9 +40,7 @@ import com.zfoo.protocol.exception.RunException;
|
|||||||
import com.zfoo.protocol.util.*;
|
import com.zfoo.protocol.util.*;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.bson.codecs.configuration.CodecRegistries;
|
import org.bson.codecs.configuration.CodecRegistries;
|
||||||
import org.bson.codecs.pojo.Conventions;
|
|
||||||
import org.bson.codecs.pojo.PojoCodecProvider;
|
import org.bson.codecs.pojo.PojoCodecProvider;
|
||||||
import org.bson.codecs.pojo.annotations.BsonId;
|
|
||||||
import org.bson.types.ObjectId;
|
import org.bson.types.ObjectId;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -105,11 +103,7 @@ public class OrmManager implements IOrmManager {
|
|||||||
allEntityCachesUsableMap.put(entityClass, false);
|
allEntityCachesUsableMap.put(entityClass, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
var pojoCodecProvider = PojoCodecProvider.builder()
|
var pojoCodecProvider = PojoCodecProvider.builder().automatic(true).register(new MapCodecProvider()).build();
|
||||||
.conventions(List.of(Conventions.ANNOTATION_CONVENTION))
|
|
||||||
.automatic(true)
|
|
||||||
.register(new MapCodecProvider())
|
|
||||||
.build();
|
|
||||||
var codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromProviders(pojoCodecProvider));
|
var codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromProviders(pojoCodecProvider));
|
||||||
|
|
||||||
var mongoBuilder = MongoClientSettings
|
var mongoBuilder = MongoClientSettings
|
||||||
@@ -441,7 +435,7 @@ public class OrmManager implements IOrmManager {
|
|||||||
var getIdMethod = ReflectionUtils.getMethodByNameInPOJOClass(clazz, FieldUtils.fieldToGetMethod(clazz, idField));
|
var getIdMethod = ReflectionUtils.getMethodByNameInPOJOClass(clazz, FieldUtils.fieldToGetMethod(clazz, idField));
|
||||||
var returnTypeOfGetIdMethod = getIdMethod.getReturnType();
|
var returnTypeOfGetIdMethod = getIdMethod.getReturnType();
|
||||||
AssertionUtils.isTrue(returnTypeOfGetIdMethod.equals(idFieldType), "[{}] getIdMethod:[{}] return type:[{}] must be equal with type id:[{}]"
|
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()方法,看看两者的返回值是不是一样的,避免出错
|
// 随机给id字段赋值,然后调用id()方法,看看两者的返回值是不是一样的,避免出错
|
||||||
var entityInstance = ReflectionUtils.newInstance(clazz);
|
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");
|
throw new RunException("orm id field only supports int long float double String ObjectId");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!idField.getName().equals("id") && !idField.isAnnotationPresent(BsonId.class)) {
|
if (!idField.getName().equals("id")) {
|
||||||
throw new RunException("use @BsonId annotation to @Id filed:[{}] or rename this field with id", idField.getName());
|
throw new RunException("@Id filed must name with id");
|
||||||
}
|
}
|
||||||
|
|
||||||
ReflectionUtils.makeAccessible(idField);
|
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;
|
package com.zfoo.orm.entity;
|
||||||
|
|
||||||
|
import com.zfoo.orm.anno.EntityCache;
|
||||||
import com.zfoo.orm.anno.Id;
|
import com.zfoo.orm.anno.Id;
|
||||||
import com.zfoo.orm.anno.Index;
|
import com.zfoo.orm.anno.Index;
|
||||||
import com.zfoo.orm.model.IEntity;
|
import com.zfoo.orm.model.IEntity;
|
||||||
import org.bson.codecs.pojo.annotations.BsonId;
|
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
@@ -24,11 +24,11 @@ import java.util.Date;
|
|||||||
/**
|
/**
|
||||||
* @author godotg
|
* @author godotg
|
||||||
*/
|
*/
|
||||||
|
@EntityCache
|
||||||
public class MailEntity implements IEntity<String> {
|
public class MailEntity implements IEntity<String> {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@BsonId
|
private String id;
|
||||||
private String mailId;
|
|
||||||
|
|
||||||
@Index(ascending = true, unique = false)
|
@Index(ascending = true, unique = false)
|
||||||
private String userName;
|
private String userName;
|
||||||
@@ -37,9 +37,9 @@ public class MailEntity implements IEntity<String> {
|
|||||||
|
|
||||||
private Date createDate;
|
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();
|
var entity = new MailEntity();
|
||||||
entity.mailId = mailId;
|
entity.id = id;
|
||||||
entity.userName = userName;
|
entity.userName = userName;
|
||||||
entity.content = content;
|
entity.content = content;
|
||||||
entity.createDate = createDate;
|
entity.createDate = createDate;
|
||||||
@@ -48,15 +48,15 @@ public class MailEntity implements IEntity<String> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String id() {
|
public String id() {
|
||||||
return mailId;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMailId() {
|
public String getId() {
|
||||||
return mailId;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMailId(String mailId) {
|
public void setId(String id) {
|
||||||
this.mailId = mailId;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
|
|||||||
Reference in new issue
Block a user