3 ms·
Reflection (which you'll be using anyway) can access private fields. See Class.getDeclaredFields. Check out JPA or Hibernate & Hibernate Annotations for the '
by justinsb 17y ago
Reflection (which you'll be using anyway) can access private fields. See Class.getDeclaredFields.
Check out JPA or Hibernate & Hibernate Annotations for the 'standard' way to do this.
I would just use JPA/Hibernate, rather than writing your own mini-ORM. But you could equally borrow the JPA Annotations and use them for your mini-ORM, and then adopt Hibernate when you're ready.
- jacquesm 17y ago> Reflection (which you'll be using anyway) can access private fields. Yes, I am using 'reflection'. That seems to be the way to get to the 'guts' of a class. Hey, that's interesting! Protected is less strict than private, if I can't see protected fields then I should be able to see private fields? I'm definitely going to see if that works, I never though that it would be able to since it couldn't read the protected ones. This is where I got that from: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html http://java.sun.com/docs/books/tutorial/java/javaOO/accessco... Maybe I messed up in a test somewhere. Or maybe I can fiddle with setAccessible from within the class the does the storing/retrieving ? Good tip on using the interface of an established library and then making the switch. Like that I can take it one step at the time.
- justinsb 17y agoJust checked in our code base: getDeclaredFields does return private fields, you then call field.setAccessible(true) to bypass security so you can then get/set the field. And this definitely works across packages - we use it for some evil tricks like peeking into private fields in the Java runtime library.
- jacquesm 17y agoSweet! Ok, so that solves my short term problem, longer term I'll have to put in a more solid ORM than this home built thing. At least it will cut down on the cruft enough that I can start to see the wood for the trees. thank you very much.
- jacquesm 17y agoOK, that was it, exactly. I was using getFields before, I completely missed the 'Declared' option.