在我的模型中,有不同的持久字段,它们具有不同的注释,如@ Column,@ OneToMany等。
@OneToMany(mappedBy = "Clinte", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Takeover> takeovers = new HashSet<Takeover>();
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "CAR_ID")
private Car carID;
@Column(length = 50, name = "TYPE")
private String type;
如何打印具有注释@Column且类型为String的持久字段?例如在我的代码中,我只想打印&#34;键入&#34;因为它有注释@Column并且类型为String。
答案 0 :(得分:1)
我找到了这样做的方法:
Field[] fields=myClass.class.getDeclaredFields();
for (int i=0; i != fields.length; ++i) {
Field field=fields[i];
Column info=field.getAnnotation(Column.class);
if (info == null || field.getType() != String.class){
continue;
}
System.out.println("Field Name : "+ field.getName() + " Field type: " +field.getType().getSimpleName());
}