Skip to content

Commit f4611d0

Browse files
committed
add #167
1 parent 633690d commit f4611d0

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/classloader/DynamicEntityClassBuilder.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.codingapi.springboot.fast.classloader;
22

3-
import com.codingapi.springboot.fast.metadata.EntityMeta;
3+
import com.codingapi.springboot.fast.metadata.EntityMetaData;
44
import jakarta.persistence.*;
55
import net.bytebuddy.ByteBuddy;
66
import net.bytebuddy.description.annotation.AnnotationDescription;
@@ -23,21 +23,21 @@ public class DynamicEntityClassBuilder {
2323
/**
2424
* 根据 EntityClass 构建动态实体
2525
*/
26-
public static Class<?> buildDynamicEntity(EntityMeta entityMeta) {
27-
if (entityMeta == null || entityMeta.getClassName() == null) {
26+
public static Class<?> buildDynamicEntity(EntityMetaData entityMetaData) {
27+
if (entityMetaData == null || entityMetaData.getClassName() == null) {
2828
throw new IllegalArgumentException("Entity metadata cannot be null");
2929
}
3030

3131
try {
3232
DynamicType.Builder<?> builder = new ByteBuddy()
3333
.subclass(Object.class)
34-
.name(entityMeta.getClassName())
34+
.name(entityMetaData.getClassName())
3535
.implement(Serializable.class)
36-
.annotateType(buildEntityAnnotations(entityMeta));
36+
.annotateType(buildEntityAnnotations(entityMetaData));
3737

3838
// 添加字段
3939
boolean hasPrimaryKey = false;
40-
for (EntityMeta.ColumnMeta column : entityMeta.getColumns()) {
40+
for (EntityMetaData.ColumnMeta column : entityMetaData.getColumns()) {
4141
builder = addColumnField(builder, column);
4242
if (column.isPrimaryKey()) {
4343
hasPrimaryKey = true;
@@ -61,25 +61,25 @@ public static Class<?> buildDynamicEntity(EntityMeta entityMeta) {
6161

6262
} catch (Exception e) {
6363
throw new RuntimeException("Failed to build dynamic entity: " +
64-
entityMeta.getClassName(), e);
64+
entityMetaData.getClassName(), e);
6565
}
6666
}
6767

6868
/**
6969
* 构建实体类注解
7070
*/
71-
private static AnnotationDescription[] buildEntityAnnotations(EntityMeta entityMeta) {
71+
private static AnnotationDescription[] buildEntityAnnotations(EntityMetaData entityMetaData) {
7272
List<AnnotationDescription> annotations = new ArrayList<>();
7373

7474
// @Entity 注解
7575
annotations.add(AnnotationDescription.Builder.ofType(Entity.class).build());
7676

7777
// @Table 注解
78-
if (entityMeta.getTable() != null) {
78+
if (entityMetaData.getTable() != null) {
7979
AnnotationDescription.Builder tableBuilder =
8080
AnnotationDescription.Builder.ofType(Table.class);
8181

82-
EntityMeta.TableMeta tableMeta = entityMeta.getTable();
82+
EntityMetaData.TableMeta tableMeta = entityMetaData.getTable();
8383
if (tableMeta.getName() != null && !tableMeta.getName().isEmpty()) {
8484
tableBuilder = tableBuilder.define("name", tableMeta.getName());
8585
}
@@ -94,11 +94,11 @@ private static AnnotationDescription[] buildEntityAnnotations(EntityMeta entityM
9494
}
9595

9696
// @Comment 注解 - 注释应该放在类上,而不是表注解上
97-
if (entityMeta.getTable() != null && StringUtils.hasText(entityMeta.getTable().getComment())) {
97+
if (entityMetaData.getTable() != null && StringUtils.hasText(entityMetaData.getTable().getComment())) {
9898
AnnotationDescription.Builder commentBuilder =
9999
AnnotationDescription.Builder.ofType(Comment.class);
100100

101-
EntityMeta.TableMeta tableMeta = entityMeta.getTable();
101+
EntityMetaData.TableMeta tableMeta = entityMetaData.getTable();
102102
commentBuilder = commentBuilder.define("value", tableMeta.getComment());
103103
annotations.add(commentBuilder.build());
104104
}
@@ -110,7 +110,7 @@ private static AnnotationDescription[] buildEntityAnnotations(EntityMeta entityM
110110
* 添加字段
111111
*/
112112
private static DynamicType.Builder<?> addColumnField(DynamicType.Builder<?> builder,
113-
EntityMeta.ColumnMeta columnMeta) {
113+
EntityMetaData.ColumnMeta columnMeta) {
114114
// 确定字段类型
115115
Class<?> fieldType = columnMeta.getType();
116116

@@ -160,7 +160,7 @@ private static DynamicType.Builder<?> addColumnField(DynamicType.Builder<?> buil
160160
* 构建字段注解
161161
*/
162162
private static List<AnnotationDescription> buildFieldAnnotations(
163-
EntityMeta.ColumnMeta columnMeta, String fieldName) {
163+
EntityMetaData.ColumnMeta columnMeta, String fieldName) {
164164

165165
List<AnnotationDescription> annotations = new ArrayList<>();
166166

@@ -170,7 +170,7 @@ private static List<AnnotationDescription> buildFieldAnnotations(
170170

171171
// @GeneratedValue 注解
172172
if (columnMeta.getGeneratedValue() != null) {
173-
EntityMeta.GeneratedValueMeta genMeta = columnMeta.getGeneratedValue();
173+
EntityMetaData.GeneratedValueMeta genMeta = columnMeta.getGeneratedValue();
174174
AnnotationDescription.Builder genBuilder =
175175
AnnotationDescription.Builder.ofType(GeneratedValue.class);
176176

springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/metadata/EntityMeta.java renamed to springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/metadata/EntityMetaData.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.codingapi.springboot.fast.metadata;
22

33
import com.codingapi.springboot.fast.classloader.DynamicEntityClassBuilder;
4-
import com.codingapi.springboot.fast.dynamic.DynamicEntityBuilder;
54
import jakarta.persistence.GenerationType;
65
import lombok.Getter;
76
import lombok.Setter;
@@ -10,13 +9,13 @@
109
import java.util.List;
1110

1211
@Getter
13-
public class EntityMeta {
12+
public class EntityMetaData {
1413

1514
private final String className;
1615
private final TableMeta table;
1716
private final List<ColumnMeta> columns;
1817

19-
public EntityMeta(String className) {
18+
public EntityMetaData(String className) {
2019
this.className = className;
2120
this.table = new TableMeta();
2221
this.columns = new ArrayList<>();

springboot-starter-data-fast/src/test/java/com/codingapi/springboot/fast/dynamic/DynamicEntityBuilderTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codingapi.springboot.fast.dynamic;
22

33
import com.codingapi.springboot.fast.entity.Demo;
4-
import com.codingapi.springboot.fast.metadata.EntityMeta;
4+
import com.codingapi.springboot.fast.metadata.EntityMetaData;
55
import jakarta.persistence.GenerationType;
66
import org.hibernate.dialect.H2Dialect;
77
import org.junit.jupiter.api.Test;
@@ -36,12 +36,12 @@ void generateTableDDL() {
3636
void dynamicGenerateTableDDL() {
3737
DynamicEntityBuilder dynamicEntityBuilder = new DynamicEntityBuilder(H2Dialect.class,"jdbc:h2:file:./test.db");
3838

39-
EntityMeta entityMeta = new EntityMeta("com.codingapi.entity.Test");
40-
entityMeta.setTable("test");
41-
entityMeta.addPrimaryKeyColumn(Long.class,"id", GenerationType.IDENTITY,"主键");
42-
entityMeta.addColumn(String.class,"name","姓名");
39+
EntityMetaData entityMetaData = new EntityMetaData("com.codingapi.entity.Test");
40+
entityMetaData.setTable("test");
41+
entityMetaData.addPrimaryKeyColumn(Long.class,"id", GenerationType.IDENTITY,"主键");
42+
entityMetaData.addColumn(String.class,"name","姓名");
4343

44-
Class<?> entityClass = entityMeta.buildClass();
44+
Class<?> entityClass = entityMetaData.buildClass();
4545

4646
List<Exception> exceptions = dynamicEntityBuilder.validatorTable(entityClass);
4747
System.out.println(exceptions);

0 commit comments

Comments
 (0)