my problem is that, i want to save class object java.lang.reflect.Field into database using Hibernate.
E.g. table:
@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;
}
}
and i have table for Conditions where i want to store java.lang.reflect.Field object via String:
@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;
}
}
and now i would do something like that:
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);
Here is my problem, now i got Exception like:
java.lang.IllegalAccessException: Class testsms.TestSms can not access a member of class tables.Actor with modifiers "private"
Probably i need to use Actor.class.getDeclaredMethods() but i dont know how. Can anyone help me to solve this problem?
Can you try
f.setAccessible(true)and then rerun your code ?