It's practically my first apprach to XML mapping with Hibernate, at least with non-basic model.
My demo pysical model (partially) would be something like:
SEASON:
SEA_ID: int -> PRIMARY KEY
SEA_DESCR: varchar
DAY:
DAY_ID: int -> PRIMARY KEY
DAY_NAME: varchar
DAY_DESCR: varchar
LEAGUE:
LEA_ID: int -> PRIMARY KEY
LEA_DESCR: varchar
MATCH: ---------- !! Attention, ternary relationship !! ----------
MTC_LEA_ID: int -> PRIMARY KEY, FOREIGN KEY 1 REFERENCES LEAGUE(LEA_ID)
MTC_SEA_ID: int -> PRIMARY KEY, FOREIGN KEY 2 REFERENCES SEASON(SEA_ID)
MTC_DAY_ID: int -> PRIMARY KEY, FOREIGN KEY 3 REFERENCES DAY(DAY_ID)
So, I have a ternary relation which I have to map in Hibernate with XML.
After researching about Hibernate and analyzing my specific problem I have chosen a solution which stores a map of Season-Days per League, restricting the access to league entities, deleting ternary and innecesary configuration and resulting in the next classes and configuration:
public class League {
Long id;
String description;
private Map<Season, Day> daysPerSeason = new HashMap<Season, Day>(0);
// Here Constructurs, Getters and Setters
}
and XML Mapping:
<hibernate-mapping>
<class name="org.tanooki.examples.example.model.entities.League" table="LEAGUE">
<id name="id" type="Integer" column="LEA_ID">
<generator class="native" />
</id>
<property name="description" type="String">
<column name="LEA_DESCR" />
</property>
<map name="daysPerSeason">
<key column="LEA_ID"/>
<map-key-many-to-many column="SEA_ID" class="org.tanooki.examples.example.model.entities.Season"/>
<many-to-many column="DAY_ID" class="org.tanooki.examples.example.model.entities.Day"/>
</map>
</class>
If I have not doing some mistake I would be able to access every day per season when I get a league entity.
But, now, at this moment, and before testing, I have two questions to solve:
1) First of all, how much clever is Hibernate regarding the naming of columns with foreign key contraints?. I mean ... in MATCH table I have a column called MTC_LEA_ID , but in LEAGUE table the related column is naming different (LEA_ID ). I'm not making explicit this relationship bettween the two names, because I have avoided the MATCH table (the ternary). I missing something?
2) About the save and delete behaviour, if first step is right or it only needs a small change, how works by default?. If I want to relate some couples of season-days to one league and execute save, the MATCH table would be updated or I need to include some cascade-type configuration?
Finally, sorry for my english and ...
Greetings
No comments:
Post a Comment