我的问题是,我想使用Hibernate将类对象java.lang.reflect.Field保存到数据库中。 例如。表:
@Entity
public class Actor implements Serializable {
@Id
@GeneratedValue
private Long id;
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
private void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
我有条件表,我想通过String存储java.lang.reflect.Field对象:
@Entity
public class Conditions implements Serializable {
@Id
@GeneratedValue
private Long id;
private String value;
private String field;
private int type;
public void setField(String field) {
this.field = field;
}
public void setType(int type) {
this.type = type;
}
public void setValue(String value) {
this.value = value;
}
public String getField() {
return field;
}
public int getType() {
return type;
}
public String getValue() {
return value;
}
private void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
现在我会做那样的事情:
Conditions con;
// con = get our condition from database (via hibernate)
java.lang.reflect.Field f = Actor.class.getDeclaredField(con.getField());
Actor a = new Actor();
a.setName("My name");
String ActorName = f.get(a);
这是我的问题,现在我得到了例外:
java.lang.IllegalAccessException: Class testsms.TestSms can not access a member of class tables.Actor with modifiers "private"
可能我需要使用Actor.class.getDeclaredMethods(),但我不知道如何。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:5)
您可以尝试f.setAccessible(true)
然后重新运行代码吗?