2011-11-13 6 views
2

私の問題は、クラスオブジェクトjava.lang.reflect.FieldをHibernateを使ってデータベースに保存したいということです。 など。表:オブジェクト "Field"を持つプライベートフィールドから値を取得します。 Java

@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; 
    } 
} 

と私は文字列経由を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; 
    } 
} 

を、今私はそのようなことだろう。

java.lang.IllegalAccessException: Class testsms.TestSms can not access a member of class tables.Actor with modifiers "private" 
:ここ

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); 
は私の問題であり、今私のような例外が発生しました

おそらく、私はActor.class.getDeclaredMethods()を使用する必要がありますが、私はそれを知っていません。誰も私がこの問題を解決するのを助けることができますか?

答えて

5

f.setAccessible(true)を試してからコードを再実行できますか?

+0

すみません、返信ありがとうございます:) –

+1

@skorek:あなたが助けたら答えを受け入れるべきです。 –

+0

私の喜びはあなたがそれをworkin持っている:) – mprabhat

関連する問題