Salute, fellow hibernators.
Synopsis:
class Acquisition subclasses class Evidence using the 'Table per subclass' strategy ('joined-subclass') using foreign key 'evidence_id'. However Acquisition has its own surrogate primary key 'acquisition_id' in addition to the foreign key evidence_id.
Is there a way of mapping these classes such that:
Code:
session.get( Evidence.class, an_evidence_id );
and
Code:
session.get( Acquisition.class, an_acquisition_id );
both work? That is, acquisitionId/acquisition_id to effectively be also considered an identifier property of Acquisition.
Secondly, and related to this mapping problem, there is the question of classes that refer to the Acquisition subclass -- how should these be mapped? Since the Scan class has only a acquisition_id foreign key to Acquisition, and not to Evidence directly. Since Acquisition is effectively the top-level object/table of a wholly separate schema/object hierachy, we'd greatly prefer being able to work with & pass around acquisition_ids rather than having to force everything back to evidence_ids.
The linked exception occurs upon Session.save( aScan ), due to the incorrect Acquisition mapping as described above.
Thanks for reading.
Matt Harrison
Specifics below.
---------------
Hibernate version: Code:
hibernate-3.2.5.ga.jar
Mapping documents:------------------ org/eurocarbdb/dataaccess/core/Evidence.hbm.xml ------------------
Code:
<class name="org.eurocarbdb.dataaccess.core.Evidence" table="evidence" schema="core">
<!-- surrogate primary key generated by db sequence -->
<id name="evidenceId" type="int" access="field">
<column name="evidence_id" />
<generator class="sequence" >
<param name="sequence">core.evidence_evidence_id_seq</param>
</generator>
</id>
...
</class>
------------------ org/eurocarbdb/dataaccess/ms/Acquisition.hbm.xml ------------------
Code:
<!-- ms.Acquisition subclasses core.Evidence -->
<joined-subclass name="org.eurocarbdb.dataaccess.ms.Acquisition"
extends="org.eurocarbdb.dataaccess.core.Evidence"
table="acquisition"
schema="ms"
>
<!-- joins evidence table on fkey evidence_id -->
<key column="evidence_id" />
<!-- this is surrogate primary key of acquisition table, defined as a regular property -->
<property name="acquisitionId" type="int" insert="false" generated="insert" access="field">
<column name="acquisition_id" />
</property>
<!-- an Acquisition has many Scans -->
<set name="scans" inverse="true">
<key>
<column name="acquisition_id" not-null="true" />
</key>
<one-to-many class="org.eurocarbdb.dataaccess.ms.Scan" />
</set>
...
</joined-subclass>
------------------ org/eurocarbdb/dataaccess/ms/Scan.hbm.xml ------------------
Code:
<class name="org.eurocarbdb.dataaccess.ms.Scan" table="scan" schema="ms">
<!-- surrogate primary key generated by db sequence -->
<id name="scanId" type="int" access="field">
<column name="scan_id" />
<generator class="sequence">
<param name="sequence">ms.scan_scan_id_seq</param>
</generator>
</id>
<!-- an Acquisition has many Scans -->
<many-to-one name="acquisition" class="org.eurocarbdb.dataaccess.ms.Acquisition" fetch="select">
<column name="acquisition_id" not-null="true" />
</many-to-one>
...
</class>
Code between sessionFactory.openSession() and session.close():Code:
Acquisition a = session.get( Evidence.class, an_evidence_id );
Scan s = new Scan();
s.setAcquisition( s );
...
session.save( s );
Full stack trace of any exception that occurs:Code:
ERROR from org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:78)
ERROR: insert or update on table "scan" violates foreign key constraint "scan_acquisition_id_fkey"
Detail: Key (acquisition_id)=(7) is not present in table "acquisition".
Name and version of the database you are using:Code:
Postgres 7.2
The generated SQL (show_sql=true):Code:
/* insert org.eurocarbdb.dataaccess.ms.Scan */
insert into ms.scan (acquisition_id, peak_processing_id, parent_structure_id, ms_exponent, polarity, deisotoped, charge_deconvoluted, base_peak_mz, base_peak_intensity, start_mz, end_mz, low_mz, high_mz, contributor_quality, scan_id)
values (7, 1, NULL, 2, 1, 1, 1, 2604.09814453125, 185.88235473632812, -1.0, -1.0, 9.92466926574707, 2748.443603515625, 0.0, 2)
Debug level Hibernate log excerpt:
(not given)