我的项目中有一些类,当我想使用myClass.newInstance()
从它们实例化对象来构建SQL查询时,它只是用一个字段创建对象而我无法检索所有类字段到完成我的查询。当我获得对象的大小时,它返回' 1'而不是' 3。
知道我该怎么办?
在这里,您可以找到一个类和我的查询构建器。
@JupiterEntity
@TableName(name="tbl_person")
public class Person {
@PrimaryKey
@DbColumn(name = "clmn_id")
private int id;
@DbColumn(name="clmn_name", length = 1024)
private String name;
@DbColumn(name="clmn_family", length = 1024)
private String family;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFamily() {
return family;
}
public void setFamily(String family) {
this.family = family;
}
}
这时我想要从我的班级实例化一个对象:
public MySQLEntityManager() {
StringBuilder stringBuilder = new StringBuilder();
if(ServiceLocator.getConfiguration().createDDL()) {
for(Class entityClass : ServiceLocator.getConfiguration().getEntities()) {
Object obj = new Object();
try {
obj = entityClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
stringBuilder.append("DROP TABLE ")
.append("'")
.append(ServiceLocator.getConfiguration().getDatabaseName())
.append("'.'")
.append(getTableName(obj)).append("' ");
System.out.println(stringBuilder.toString());
stringBuilder.setLength(0);
stringBuilder.append("CREATE TABLE")
.append("'")
.append(ServiceLocator.getConfiguration().getDatabaseName())
.append("'.'")
.append(getTableName(obj)).append("' ");
Map<Field, Object> fields = ReflectionHelper.getValuesOfFields(obj);
for(Field field : fields.keySet()) {
System.out.println(field.getName() + " -> " + fields.get(field));
}
String delimiter = "";
for (Field field : fields.keySet()) {
String columnName = field.getName();
if (field.isAnnotationPresent(DbColumn.class)) {
DbColumn dbColumn = field.getAnnotation(DbColumn.class);
columnName = dbColumn.name();
}
stringBuilder.append(delimiter).append(columnName);
delimiter = ", ";
}
System.out.println("*****" + stringBuilder.toString());
delimiter = "";
System.out.println(stringBuilder.toString());
}
}
}
答案 0 :(得分:0)
所以问题是ReflectionHelper.getValuesOfFields(obj)
没有返回你想要的东西。你可以调试它,或使用obj.getClass().getDeclaredFields()
而不是一些NIH垃圾。