How can sleep mode go into a private field?

How can hibernate access a private field / method of a java class, for example, to set @Id?

thanks

+5
source share
3 answers

As Crippledsmurf says, he uses reflection. See Reflection: Violation of all rules and Hibernation: saving contract object .

+9
source

Try

import java.lang.reflect.Field;

class Test {
   private final int value;
   Test(int value) { this.value = value; }
   public String toString() { return "" + value; }
}

public class Main {
   public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
       Test test = new Test(12345);
       System.out.println("test= "+test);

       Field value = Test.class.getDeclaredField("value");
       value.setAccessible(true);
       System.out.println("test.value= "+value.get(test));
       value.set(test, 99999);
       System.out.println("test= "+test);
       System.out.println("test.value= "+value.get(test));
   }
}

prints

test= 12345
test.value= 12345
test= 99999
test.value= 99999
+4
source

,

I am not a Java programmer, but in my opinion java has reflection support similar to the .NET support I use

+3
source

All Articles