Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this a good approach for a custom object-relational mapping in Java?
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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 <code>getManager()</code> method), the latter will be lazily loaded. In code:</p> <pre><code>public class Employee { private int id; private int managerId; private Employee manager; public int getId() { return id; } public Employee getManager() { if(manager == null &amp;&amp; managerId &gt; 0) { // Lazy loading! } return manager; } public void setManager(Employee manager) { this.manager = manager; this.managerId = manager.getId(); } } </code></pre> <p>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.</p> <p>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).</p> <p><strong>EDIT:</strong> Just to clarify, my architecture for this effort (such as it is) doesn't involve Java EE at all. It's a Java SE architecture where the client interfaces with the server via straightforward RMI.</p> <p>Also, the Employee class above clearly does need a reference to a DAO to perform the lazy loading. However, I don't see this as necessarily tightly coupled because:</p> <ol> <li>the Employee object could hold a reference to an abstract interface, which the DAO implements; and/or</li> <li>the Employee object could hold a reference to the server object, which in turn holds a (private) reference to the DAO.</li> </ol> <p>Note that in the case of #2, the server object that the Employee object references is presumably the same one that the client references.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload