The subject doesn't quite say it all but here goes. Some notes, I'm porting an existing project to hibernate so that I can easily extend support to different DBMS. I'm not an expert on database design but know a bit.
Code:
public abstract class AbstractDrugModel
{
// Not null. Uniuqe.
private String name = "";
// Can be null.
private IntravascularDrug ivDrug = null;
// Not null
private Equation outputEquation = null;
// 0 or more.
private List<DrugEvent> drugEvents = new LinkedList<DrugEvent>();
}
// Not the only subclass but sufficient for examples.
public class UserDefinedDrugModel
{
// No properties.
}
public class IntravascularDrug
{
// Some primitive properties.
private int minimumInfusionTime = -1;
}
public class DrugEvent
{
// The event name. Unique. Not null.
private String eventName = "";
// The index is important because it is used in other equations to reference this. Starts at 1.
private int index = 0;
// Other uninteresting properties.
}
Before hibernate:
I had a drug table to store the AbstractDrugModel. The primary key was the drug name which is unique and not null.
I had a DrugEvent table that stored the DugEvent properties. This had a foreign key to the drug name.
I had an IVDrug table that stored the IntravascularDrug instance if it existed. This had a foreign key to the drug name.
I had an OutputEquation table that contained the properties of the outputEquation and had a foreign key to the drug name.
From the design above I didn't need any of DrugEvent, IntravascularDrug or Equation to "contain" a reference to the AbstractDrugModel instance that owned it. I also didn't need any database identifiers for these as the drug name was suffient when storing in the database.
In my hibernate version I am using mapping files. I have used the drug name as the id field in the table for AbstractDrugModel. However I have not figured out with my original model how to use the drug name as a foreign key and this also as the the combined key with, for example, the drug event name.
In my current solution I've introduced an identifier to each of DrugEvent, IntravascularDrug and Equation. I've also added a reference in DrugEvent back to the AbstractDrugModel that owns it. I would prefer to avoid doing this, I'm open to suggestions however. I don't mind having DrugEvent reference back to AbstractDrugModel but the identifiers are not really needed.
In addition, my current mapping files now include in the table for the AbstractDrugModel table a column for the contained IntravascularDrug, as an example.
Here are some snippets:
Code:
<class
dynamic-insert="false"
dynamic-update="false"
lazy="false"
mutable="true"
name="tciworks.drugmodel.AbstractDrugModel"
optimistic-lock="version"
polymorphism="implicit"
select-before-update="false"
table="Drug">
<list cascade="all-delete-orphan" name="eventList" table="DrugEvent">
<key column="DrugName" not-null="true" update="false"/>
<list-index column="EventIndex"/>
<one-to-many class="tciworks.drugmodel.DrugEvent"/>
</list>
<many-to-one
entity-name="OutputEquation"
name="outputEquation"
column="OutputEquationIdentifier"
not-null="true"
unique="true"
lazy="false"
cascade="all"/>
<many-to-one
name="intravascularDrug"
column="IntravascularDrugIdentifier"
not-null="false"
cascade="all"
unique="true"/>
</class>
The other mapping files don't mention the drug name, I'm not sure how to do this or if it can be done.
I hope this makes some sort of sense about what I'm trying to achieve. If not I will try to elaborate with one of the problems at a time.