-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from sandogeek/main
feat[orm]: id filed can use other name with @BsonId or @id annotation
- Loading branch information
Showing
5 changed files
with
58 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
orm/src/main/java/com/zfoo/orm/convention/ZfooAnnotationConvention.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters