• Post Reply Bookmark Topic Watch Topic
  • New Topic

What do you think of Java Reflection, am I been a bad boy ?  RSS feed

 
Ricardo Coto
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm questioning my self if there's a good idea, a professional idea to use reflection...

Context
We're in the middle of an optimization change to get the DB response times lower..
In order to achieve this, we're using two persistent contexts, because we're using a Read Only database (to migrate all fetches/gets to read only db), so, as we have defined these two PC's in our code, there are some cases, specially in UPDATES and DELETES, where the code actually fetches for an item in Database through hibernate, then the same object is updated, and then committed, the thing is, we analyze what operation is performing before commit something, in order to know what PC to use, if the nature of the action is reading, we use PC Read Only, if not we use Write DB...

Thing is, when we fetch the object, it's fetched using PC-Read, and when it update/delete, the object belong to the Read PC, and it throws an error, so in order to avoid that, we use reflection, to update the reference of the object (I know it hits the DB twice, but the second hit is cheap as it is over an id)



As you can see, we're using reflection to get the object (o) id and then, create an object with the associated PC (write) then remove.

Is this right ? is this a good practice, if it's a not common used practice is it acceptable in some way, is it a workaround instead of reflection for this cases?

Thanks for your help....
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you are always assuming that the object has a getId() method, is this not something that could be done via an interface?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. And, in your code, what happens if the object doesn't have such a method?
 
Ricardo Coto
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes we're sure objects are going to have getId, they are entities, and as a convention they will have it...

in what way we can achieve this through an interface ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have all objects implement the interface, cast the object to the interface, call getId().
 
Ricardo Coto
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember these are entities, hibernate isn't going to let me i think, besides, remember this is an optimization, that is totally invasive, having cents of classes changed it's not reliable...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I implement interfaces on entities all the time. How is Hibernate preventing you?

And, "not reliable"?
 
Ricardo Coto
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not saying it's not reliable by it self, i said is not reliable how invading this is, remember this is an optimization, there's a bunch of entities there, imagine you changing every entity present in the project adding an inferface to each one on them, that's what i say is not reliable, the change is huge, so i would take other paths before doing something like this...

Remember hibernate have a process of mapping, i tried in past time in other project, not this one, to implement an interface in hibernate entities and had weird errors...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!