Cheers, Martijn,
Twitter.
Martijn Verburg wrote:
Firstly a simple solution would be to ensure that the state can only be changed by a public setter method. In that setter method you could perform extra logic that confirms whether the state has changed (if new value is not equal to current value then state has changed).
Ulf Dittmer wrote:What are you trying to achieve by using something complicated, when simple solutions are available?
Cheers, Martijn,
Twitter.
Treimin Clark wrote:
Yes I know; but I'm not going to build a real application now. I just like to learn that.
Cheers, Martijn,
Twitter.
Martijn Verburg wrote:I'd still strongly recommend the Observer pattern.
I'd start a new thread in this forum with your Reflection specific question
Ulf Dittmer wrote:
Or you could just take it from Martijn (and myself) that using reflection for this is not a good idea (I'm not even sure how you propose to use it in this case - if those fields can be set directly, then they can be read directly; what's the point of using reflection?).
Cheers, Martijn,
Twitter.
Martijn Verburg wrote:Well, I'd design it so that the Object needs to inform the Entity Manager!
Cheers, Martijn,
Twitter.
Martijn Verburg wrote:Another approach is to have a 2nd thread acting as an Entity Monitor, it checks every x seconds if state has changed on an object and if it has then it informs the Entity Manger. Not this could be an expensive operation (checking each object, holding onto a reference to each object etc, etc).
Martijn Verburg wrote:The 2nd thread would need to have access to a Map of all of the objects yes (this is why it's potentially inefficient).
Cheers, Martijn,
Twitter.
Treimin Clark wrote:
Can someone imagine a possible implementation now?
Treimin Clark wrote:Do you think that I'm going to make an impossible application?
Well, the EntityManager application which I mentioned above is a real world application (java class) in the EJB Object/Relational mapping. It can persist any entity class to it. And then, the changes made to the entity will be written to the database automatically.
I didn't post this thread in neither object/relational mapping forum or EJB forum, because I need to know "how" EntityManager is implemented by using Java. (So there are nothing talking about EJB or O/R mapping here).
If I tell you again,
I highly believe that EntityManager uses Java Reflection api or any other 'miracle' api to do this task.
Steve
Ernest Friedman-Hill wrote:
Instead, what Hibernate and JPA actually do is only look at the data in the objects when they actually need to.
Treimin Clark wrote:
If it is, to implement that, JPA will automatically write a .java file implicitly including the getAddress() method, automatically and implicitly compile it and execute it to get the "address" from the entity (Just like JSP does). Is this correct? Otherwise, how do it invoke the getAddress() method on the entity?
Ernest Friedman-Hill wrote:
It could generate and compile Java source, or generate bytecodes directly; or it could use reflection. Reflection can be used to call methods whose names you find out only at runtime, or access variables at runtime. It can not be used to get automatic notification when a variable changes -- which is what I thought you were asking about. All it can do is read the value of a variable, when you ask it to.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |