I'm seeking advice for how to load joined-subclasses where the reference is reversed and located in the subclass table.
The situation: A student network is modeled in the database by following tables (there are more):
Code:
-- Computers
TABLE Computers (id SERIAL PRIMARY KEY, hostname VARCHAR);
-- Outlets found in a student's flat (and other rooms)
TABLE Outlets (id SERIAL PRIMARY KEY);
-- Ports in a switch
TABLE SwitchPorts (id SERIAL PRIARY KEY);
This is how things may be connected:
1) A Computer may be connected directly to a SwitchPort, OR
2) A Computer may be connected to an Outlet.
3) An Outlet may be connected to one or more SwitchPorts.
4) An Outlet may be connected to one or more Computers.
5) A SwitchPort may be connected directly to a Computer, OR
6) A SwitchPort may be connected to an Outlet, OR
7) A SwitchPort may be connected to another SwitchPort.
Comments:
1. There are serverrooms.
2. This is how most computers are connected.
3. In the old days when SOHO-switches were expensive, one physical TP-cable was split at the ends using all eight wires into two RJ45 sockets. So, in some cases we have the situation where one flat occupies two ports in a switch. (Some of the old switches couldn't accept more that one MAC which is why this at all had to be done.)
4. With most cases there's only one computer in each flat, but:
- we have the situation with the "split cable"
- users may use there own SOHO-switches to connect more than one computer.
5. Serverrooms.
6. The cable leading from a wiring closet to a flat's outlet.
7. Uplink/Downlinks.
This "wiring" is modeled with these tables (
graphics):
Code:
-- CAT5, CAT6, MultiMode, SingleMode, MutliMode/SingleMode double
TABLE NetworkCableTypes (id SERIAL PRIMARY KEY, name VARCHAR);
-- The actual cables
TABLE NetworkCable (
id SERIAL PRIMARY KEY,
type FK NetworkCableTypes
);
-- Cables going from ports to ports
TABLE Port_Port (
id SERIAL PRIMARY KEY,
cable FK NetworkCable NOT NULL,
uplink FK SwitchPort NOT NULL,
downlink FK SwitchPort NOT NULL
);
There are also tables for Computer_Outlet, Outlet_Port, Computer_Port which look like Port_Port above.
The hibernate-mapping file NetworkCable.hbm.xml:
Code:
<hibernate-mapping>
<class name="NetworkCable" table="NetworkCables">
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">NetworkCables_id_seq</param>
</generator>
</id>
<property name="note" type="text" />
<many-to-one name="cableType" />
<joined-subclass name="NetworkCableComputerPort"
table="Computer_Port">
<many-to-one name="computer" />
<many-to-one name="port" />
</joined-subclass>
<joined-subclass name="NetworkCableOutletPort"
table="Outlet_Port">
<many-to-one name="outlet" />
<many-to-one name="port" />
</joined-subclass>
<joined-subclass name="NetworkCablePortPort"
table="Port_Port">
<many-to-one name="uplink" />
<many-to-one name="downlink" />
</joined-subclass>
<joined-subclass name="NetworkCableComputerOutlet"
table="Computer_Outlet">
<many-to-one name="computer" />
<many-to-one name="outlet" />
</joined-subclass>
</class>
</hibernate-mapping>
Hence, the relationships for Computer, SwitchPort and Outlet should be:
A) A Computer may only have one cable. (one-to-one)
B) A SwitchPort may only have one cable.
C) An Outlet may have many cables.
I've failed to realize how to define mappings files for these classes, where their cable(s) are loaded automatically by Hibernate.
So possible solutions I've came up with so far:
S1: Implement LifeCycle and save the reference to the Session. When getCable[s]() is called, I manually load these NetworkCable* objects using the Session.
S2: Change the database model to use _one_ table:
Code:
TABLE NetworkCables (
id SERIAL PRIMARY KEY,
type FK NetworkCableTypes (id),
computer FK,
port FK,
outlet FK,
uplink FK,
downlink FK
);
Comments:
S1 seems cumbersome and I have no experience of how to implement Lifecycle for this "after onLoad() loading".
S2 maybe isn't such a bad idea, only need to migrate the data.
Other solutions? Anything I've missed about the mapping that would make hibernate load the Cable(s) into Computer/SwitchPort/Outlet?
Thanks in advance,
Fredrik