Hi,
I'm new to Hibernate (which I'm enjoying -- honestly!) and have a question regarding an association between two class hierarchys, which are mapped to a table-per-class hierarchy strategy. (see bad uml below for class association :-)
Code:
______________ _______
| Session | * 1 | State |
|--------------|-----------|-------|
| long id | | |
| Date lastMod | | |
| State state | |_______|
|______________| ^
^ |
| State
Session subclasses
subclasses
I.e. each instance of a`session' type has a state, which is a subclass of the abstract `state' class. Each state can belong to zero or more sessions.
What's puzzling me is how I map this in Hibernate... I've tried using a <one-to-one/> mapping with a foreign key in the state table of the session id (mapping file attached below, which contains one subclass of each class hierarchy), but when I call hibernate to save(session_instance), nothing gets saved to the state table.
What I'd *really* like to have is both hierarchies in one table, so that a session_type also had a state_type, but I don't think this is possible - maybe I'm wrong though!
I'm sorry if this is a trivial problem, or if I've got this completely wrong -- but I'm a bit stuck about how to represent this and get it working in hibernate...
Many thanks for any help...
Ed.
Hibernate version: 3.1.2
Mapping documents:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
the mapping for the class hierarchy of session types.
uses a "table-per-class-hierarchy" model as desribed on
page 99, Section 3.6.2 of Bauer.
each session has four attributes: its id, last updated
time, type and current state. a session can only have
one state.
-->
<hibernate-mapping>
<class
name = "session"
table = "sessions"
discriminator-value = "SESSION">
<!-- 1) unique session id -->
<id name = "id"
column = "SESSION_ID"
type = "long" />
<!-- 2) type of session -->
<discriminator
column = "SESSION_TYPE"
type = "string"
not-null = "true" />
<!-- 3) last update time -->
<property
name = "lastUpdateTime"
column = "LAST_UPDATE"
type = "java.util.Date"
not-null = "true" />
<!-- 4) current state -->
<one-to-one
name = "state"
class = "State"
cascade = "delete"
constrained = "true" />
<!-- subclasses of session -->
<!-- session over tcp -->
<subclass name = "TcpSession"
discriminator-value = "TcpSession" />
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
the mapping for the class hierarchy of session states.
-->
<hibernate-mapping>
<class name="State"
table="states"
discriminator-value="STATE">
<!-- session id -->
<id name="id"
column="SESSION_ID">
<generator class="foreign">
<param name="property">sessions</param>
</generator>
</id>
<discriminator
column = "STATE_TYPE"
type = "string" />
<subclass
name = "Uninitialised"
discriminator-value = "Uninitialised" />
</class>
</hibernate-mapping>
Name and version of the database you are using:
mySql 4.1.18
The generated SQL (show_sql=true):
insert into agreements (LAST_UPDATE, SESSION_TYPE, ID) values (?, 'TcpSession', ?)