Reputation: 11
I work with Hibernate 4.3, using hmb.xml files and I need to access existing Oracle database.
Tables exemple :
Dossier :
id_dossier(int),
noDossier(int),
... ,
D_Crea(Date),
U_Crea(Varchar),
D_Maj(Date),
U_Maj(Varchar);
Adresse :
id_adresse(int),
rue(varchar),
...,
D_Crea(Date),
U_Crea(Varchar),
D_Maj(Date),
U_Maj(Varchar);
Contrat :
id_contrat(int),
d_signature(date),
...,
D_Crea(Date),
U_Crea(Varchar),
D_Maj(Date),
U_Maj(Varchar);
I can't change any thing on the DB structure.
POJOs :
abstract class CUObject{
private int id;
private Date createDate, updateDate;
private String createUser, updateUser;
}
class Dossier extends CUObject{
private String noDossier;
}
class Adresse extends CUObject{
private String rue;
}
class Contrat extends CUObject{
private Date signatureDate;
}
Changes on POJO's structure are possible.
Afert reading Hibernate documentation, the best way to map this hierrarchy seems to be using class and union-subclass :
<class abstract="true" name="CUObject">
<id />
<property column="D_CREA" name="createDate"/>
<property column="D_MAJ" name="updateDate"/>
<property column="U_CREA" name="createDate"/>
<property column="U_MAJ" name="updateUser"/>
<union-subclass name="Dossier" table="DOSSIER" >
<property column="NO_DOSSIER" name="numDossier" />
</union-subclass>
<union-subclass name="Addresse" table="ADRESSE" >
<property column="RUE" name="rue" />
</union-subclass>
<union-subclass name="Contrat" table="CONTRAT" >
<property column="D_SIGNATURE" name="signatureDate" />
</union-subclass>
</class>
But, like mentioned in documentation, "the column name must be the same on all subclass tables. The identity generator strategy is not allowed in union subclass inheritance. The primary key seed has to be shared across all unioned subclasses of a hierarchy."
This is a problem for me : I need a generator strategy for each subclass (oracle sequence), the primary key is not shared between subclasses, and the primary key has not same name across all subclasses.
What other approach to map CU fields without making horrible copy / paste on the mapping of each class?
Tanks a lot. PS : I use this site for several years now. I want to thank you for all the answers you've given me so far.
Upvotes: 0
Views: 890
Reputation: 11
I finally found an acceptable solution that reads the existing tables and avoid copy / paste in mapping files : do not declare inheritance in the mapping, and use the concept of XML ENTITY to include multiple times sames tags.
Mapping.hmb.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"[
<!ENTITY genericProperties SYSTEM "{classPath}orm-mapping/genericProperties.xml">
]>
<hibernate-mapping>
<class name="aa.bb.ccc.shared.model.customer.Dossier" table="DOSSIER">
<id column="ID_DOSSIER" name="id">
<generator class="sequence-identity" >
<param name="sequence">SEQ_DOSSIER</param>
</generator>
</id>
&genericProperties;
<property column="NO_DOSSIER" generated="never" lazy="false" name="numDossier" />
</class>
</hibenate-mapping>
genericProperties.xml (no XML header or doctype, just RAW) :
<property column="D_CREA" generated="never" lazy="false" name="createDate" type="timestamp"/>
<property column="D_MAJ" generated="never" lazy="false" name="updateDate" type="timestamp"/>
[...]
It does the job, but I'm open to a cleaner solution.
Upvotes: 1