Skip to content

Commit

Permalink
feat[orm]: id filed can use other name with @BsonId annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 29, 2024
1 parent 4bc8650 commit e6544ea
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
14 changes: 10 additions & 4 deletions orm/src/main/java/com/zfoo/orm/manager/OrmManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
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;
Expand Down Expand Up @@ -103,7 +105,11 @@ public void initBefore() {
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
Expand Down Expand Up @@ -435,7 +441,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 @@ -456,8 +462,8 @@ private void checkIdField(Class<?> clazz) {
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);
Expand Down
41 changes: 41 additions & 0 deletions orm/src/test/java/com/zfoo/orm/accessor/IdAnnotationTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
20 changes: 10 additions & 10 deletions orm/src/test/java/com/zfoo/orm/entity/MailEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@

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
private String id;
@BsonId
private String mailId;

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

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;
Expand All @@ -48,15 +48,15 @@ public static MailEntity valueOf(String id, String userName, String content, Dat

@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() {
Expand Down

0 comments on commit e6544ea

Please # to comment.