Reputation: 17 Tradesman
- Active Posts:
- 676 (0.32 per day)
- 26-May 08
- Profile Views:
- Last Active:
- Oct 01 2013 05:09 AM
- OS Preference:
- Favorite Browser:
- Favorite Processor:
- Favorite Gaming Platform:
- Your Car:
- Who Cares
- Dream Kudos:
Posts I've Made
Posted 16 Jul 2012
Posted 16 Jul 2012It's really not reflection's fault; it's the methodology. You'd only need to use reflect once per class. How long does that take? Now, if you did it for every single transaction, now we're talking overhead.
Thanks for this advice, its really helpful and has a really clear point.
jon.kiparsky said:Events.java has fields date, time, place, participants, description, maybe a few other fields. These are stored as Java-flavored objects.
The object has methods to import and export its fields by writing a query to a database (presumably it gets the DB access from the running class), and it can also populate itself by whatever other means you find useful, ie, it can read a line from a flat data file if you need, or take direct keyboard input, or whatever.
So its a single object that handles everything related to it, very OOP approach. It's a great idea, I'll just have to mix this with our current MVC structure and see what I can came up with.
Posted 15 Jul 2012@baavgai
baavgai said:There are a number of pitfalls in an automatic approach. Not the least of which are any number of edge cases that will blow you out of the water. What if you need to hold variable that isn't stored in the data base? What if the field names can't match due to DB restrictions? What is the identity?
The fields on the database tables is exactly the same with those Plain Java Objects that's why I want to automate it using Reflection. The PK is automatically generated and spring already has a way of retrieving them. My main problem is "REFLECTION WILL GREATLY DEGRADE PERFORMANCE" even if I used it as minimal as possible(Just for generating SQL Strings).
jon.kiparsky said:This does seem like a dubious proposal to me. If I understand you correctly, you want to pass in random objects and write them to your DB using the field name as the key?
I don't know why dubious means but I'm actually passing Plain Java Objects that has corresponding table on the database with the same fields. eg: Events.java = Tbl_events ... Class attributes = Table Fields.. PK are auto-incremented values and Spring JDBC has decent way of retrieving them during adding new record.
pbl said:What don't you simply save your Object as a Java object in the DB ?
No need to separate in different columns each of the fields. So if a field is added or suppressed it won't matter. If added old serialized object will have them initialized to 0,false, null. If deleted, they will just be ignored.
I'm not quite sure what you meant by this. Is this method advisable on a Relational Database Driven application?
Ghlavac said:To get around the issues of field names with database I would suggest you add some annotation stuff to your classes, make your own and then access those with reflection to refine the bindings enough to be less irritating.
Have you tried doing this? does it have noticeable performance issue?
Posted 11 Jul 2012The performance overhead of reflection is sometimes exaggerated. I can only say, if in doubt, benchmark it.
How do I do that benchmarking? is there a tool? method or a standard practice? or just plain manual observation?
Quotee.g. (a quick google) THIS
Where currently using that and this is how we do it.
String sql = "insert into t_actor (first_name, last_name, address) values (?, ?, ?)" this.jdbcTemplate.update( sql,"Leonor", "Watling", "Some address..");
That's why I want to dynamically generate SQL string since its very tedious to code it and those parameters manually. Then I was told that Reflection would degrade performance.
Posted 11 Jul 2012
Quoteis there any other possible better way of accomplishing this without using Reflection?
Well, you can obviously only avoid reflection if your code limits itself to objects where the internal organisation is known. That will make your persistence code inflexible by definition.
This code below is the only part where I'll be using reflection. Will this line really degrade performance or using reflection is really a bad idea?
Field fields = obj.getClass().getDeclaredFields();
About not using Hibernate or ORM. I just can't use them since I was told to use plain JDBC in Spring. I don't intend on re-inventing anything, I just want to dynamically create SQL string based on Plain Java Objects.
- Member Title:
- I Desire...
- 28 years old
- November 15, 1985
- Reading Ebooks, Learning Computer Related Stuff, Photo Editing, Graphics, 3D modeling, animation, Programming, anything about computers except history..
- Full Name:
- Romar Ybañez
- Years Programming:
- Programming Languages: