Results 1 to 6 of 6
- 12-14-2010, 12:00 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Is this a good approach for a custom object-relational mapping in Java?
I'm developing a sample Java application that uses Oracle's built-in "HR" database schema. I'd like to treat table rows as Java objects. One of the tables in the HR schema, EMPLOYEES, is self-referential. It has a MANAGER_ID column which contains (if not NULL) a foreign key to another row in the EMPLOYEES table.
In object-oriented terms, the problem is how to get access to a given Employee object's manager (which is another Employee object). Eager loading doesn't seem to be a good solution, since the given Employee object's manager could also have a manager, and so on. The number of objects to eagerly load in this situation is unbounded.
What I've decided to do so far is eagerly load the MANAGER_ID field when instantiating an Employee object. Then, when the Employee's manager Employee object is requested (through a getManager() method), the latter will be lazily loaded. In code:
So is this a good approach? The only issue I have with it is how to implement the lazy loading. It would seem that an Employee object needs a reference to its instantiator -- presumably that would be the same thing that would instantiate its manager Employee object.Java Code:public class Employee { private int id; private int managerId; private Employee manager; public int getId() { return id; } public Employee getManager() { if(manager == null && managerId > 0) { // Lazy loading! } return manager; } public void setManager(Employee manager) { this.manager = manager; this.managerId = manager.getId(); } }
Also, I know I could just use one of the many ORM frameworks out there instead of rolling my own, but I'm doing this on my own to gain more insight into the underlying process(es).
- 12-14-2010, 04:34 AM #2
its kind of clunky, and not ideal to have the data access object's reference stuffed into the data bean, but perhaps you could stuff a reference to the DAO or what ever it is that invokes the SQL query into this bean, so that when the bean has a reference to the data accessor, it would be able to in the
Java Code:private transient EmployeeDAO employeeDao; /** * so add a second constructor that optionally allows the data accessor to stuff itself into the bean when it is creating the bean. * (We still have the noargs constructor for just creating a bean in the application right.) */ public Employee(EmployeeDAO employeeDao) { this.employeeDao = employeeDao; } public Employee() { // empty } public Employee getManager() { if(manager == null && managerId > 0) { if (employeeDao != null) { manager = employeeDao.findEmployeeById(managerId); } else { // whoops, lazy loading failed. } } return manager; }
- 12-14-2010, 03:14 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Thanks for your reply, Travishein!
Actually, this effort doesn't involve Java EE at all. I thought I'd do something more basic first to get my feet wet. So my plan is to develop something with regular RMI and JDBC.
With that in mind, does my ORM approach seem better, worse, or neither? :)
- 12-15-2010, 12:31 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Another way of doing this would be to simply store the foreign keys alone in the business objects. Since the client already has a connection to the server, it could fetch up the related objects on demand. Would that work better? At the very least, it seems more straightforward.
- 12-15-2010, 12:57 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I like your lazy-loading approach. Maybe have another class that does the actual retrieval from the database, and caches the Employee objects in a HashMap as it retrieves them. This other class would have a getEmployeeById() method, which would check its HashMap cache first, then pull from the database if necessary. Would that help?
-Gary-
- 12-15-2010, 02:22 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 4
- Rep Power
- 0
Thanks! Do you mean the approach I described in the OP, or the one in my last post? (I'm leaning towards the former, but I could be wrong.)
I like your idea about caching Employee objects that have been loaded from the database. That would help decouple the RMI server from the data-access code. If memory usage becomes an issue, the cache could have a size limit and the least-recently-used Employee objects could be removed from the cache once it becomes full.
How does that sound?
Similar Threads
-
Cast object to custom class problem
By trader5050 in forum New To JavaReplies: 7Last Post: 11-15-2010, 10:47 AM -
Efficient Bi-Directional Mapping Between Object References and IDs?
By kreyszig in forum Advanced JavaReplies: 2Last Post: 10-20-2010, 12:29 PM -
Hello Good Morning, Good afternoon, and Good Evening
By MrFreeweed in forum IntroductionsReplies: 3Last Post: 12-11-2009, 03:32 PM -
Consuming web service with return custom object
By dream_ in forum NetworkingReplies: 1Last Post: 04-16-2009, 06:56 AM -
mapping java class with xml
By k2kcall in forum XMLReplies: 0Last Post: 03-27-2009, 09:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks