Hello. I was hoping to get some advice on my situation. I'm new to the forum and hibernate, and i've been racking my brain for days for a solution, so any help would be much appreciated.
It's a little difficult to summarize, but it involves a Code Table, locale based descriptions. Just wondering if I can set up a hibernate mapping file to work with my current database design.
A little info on our
database. We have two tables we're using at the moment:
ChangeReason and
StandardText. Here's what they look like:
CHANGE_REASONCHANGE_REASON_ID
CHANGE_REASON_CODE
STANDARD_TEXTSTANDARD_TEXT_ID
STANDARD_TEXT_CODE
STANDARD_TEXT_DESCRIPTION
LANGUAGE_ID
STANDARD_TEXT_ID
The java class is as follows:
Code:
public class ChangeReason {
private Integer id;
private String code;
private Map<Locale, String> descriptions;
// standard constructors, getters, setters
Notice that there is no java class for StandardText, as it's only used to store locale based text/descriptions in the DB. From a business POV, the descriptions are stored in the ChangeReason java object.
Here's my hibernate mapping file:
ChangeReason.hbm.xmlCode:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="canada.borderservices.tphs.models.codes.ChangeReason"
table=""changeReason"" schema=""TPHS"">
<id name="id" column=""changeReasonID"">
<generator class="sequence">
<param name="sequence">"TPHS"."changeReason_changeReasonID_seq"</param>
</generator>
</id>
<property
name="code" column=""changeReasonCode"" />
<!-- This code uses the changeReasonId, and inserts it in the StandardTextCode field in StandardText table-->
<map name="descriptions" table=""standardText"" lazy="false">
<key column=""standardTextCode"" />
<index column=""languageID"" type="locale" />
<element column=""standardTextDescription"" type="string" />
</map>
</class>
</hibernate-mapping>
So far, when creating a new ChangeReason, it inserts a row in the StandardText table for each (key, value) pair in the ChangeReason.descriptions attribute in the java class. In addition to what it does currently, each time a new StandardText entry is inserted, we need to set the value of the STANDARD_TEXT.STANDARD_TEXT_TYPE column to "changeReason" or something like that so that the row is marked as being part of the ChangeReason group.
Is there code I can put in the mapping file that will do what I need? i.e. for each row inserted, fill the value of the "STANDARD_TEXT_TYPE" to ChangeReason?
Or do I need to write specific HQL statements for such a thing? Or possible an interceptor? I've read about both of those things, but don't know exactly how it will help solve my problem.
Thanks in advance
Chris