views:

1408

answers:

3

I have the following Hibernate Mapping, which has to be mapped using the Table per Concrete Class Hierarchy:

<hibernate-mapping package='dao'>
    <meta attribute='class-description'></meta>
    <class name='PropertyDAO'>
     <id name='id' column='id_property'>
      <generator class='assigned'/>
     </id>
     <property name='address' column='address' type='string'/>
     <union-subclass name='HouseDAO' table='house'>
      <property name='noOfRooms' column='noOfRooms'/>
      <property name='totalArea' column='totalArea'/>
      <property name='price' column='price'/>
     </union-subclass>
     <union-subclass name='LandDAO' table='land'>
      <property name='area' column='area'/>
      <property name='unitPrice' column='unitPrice'/>
     </union-subclass>
    </class>
</hibernate-mapping>

Which means in the database i have only 2 tables :

  • house (id_property(PK), address, noOfRooms, totalArea, price)
  • land (id_property(PK), address, area, unitPrice)

As far as I understood, in this case the ids need to be generated explicitly before calling .save(), so my question is: How can I create a strategy for the automatically generation of the ids, so that the ids from the concrete class form a continuous domain when joined.

+1  A: 

I used the following with joined-subclass

<key column="id"/>

This should make subclasses use the ids from the property table since that is reliant on it. (the super) The column refers to the column in the sub class that is a foreign reference to the super.

Arthur Thomas
Your solution is good for the table per subclass hierarchy, but this isn't my case, as I have only 2 tables : house and land
melculetz
+2  A: 

IMHO your model in the DB is wrong as you have redundant information across multiple tables which are related.

Table per concrete class is an inheritance model which gives problems at runtime as you can have the situation where one updates the address of Land but not of House while they're the same (semantically). I.o.w.: drop this model and introduce table-per-subclass, so you have a property base table with id and address and two separated tables with a PK which is an FK to the pk of property base, one is house with the house specific fields, the other is land with the land specific fields.

That will give you the smallest number of problems as it's the way to convert inheritance between entity types to relational tables (see Nijssen/Halpin's books about NIAM/ORM)

Frans Bouma
Thanks very much, but unfortunately I cannot change the database structure, so I was looking for a solution for the Table per concrete class model
melculetz
A: 

The solution is to create another table that stores the next id; this value should be modified each time a user wants to insert a new entity. In this way, the domain is continuous

melculetz