I've got a quick question.
I have an object which extends the behavior of the GregorianCalendar class, and was having issues getting it to persist. The problem arises when it comes to selecting a datatype field in the hibernate mapping file. If I choose to go with specifying the type of my sublclass, then hibernate looks for a binary field. If I specify it as a calendar_date field, then, at reflection time, it sees the subclass in the method signature and freaks out.
What I have been doing to get around this is making the hibernate mapping entries like such...
Code:
<class name="com.sccc.puncher.domain.Day" table="day">
<id name="id" column="id" type="long" unsaved-value="null">
<generator class="identity" />
</id>
<many-to-one name="week" column="week_id"
class="com.sccc.puncher.domain.Week"/>
<property name="[u]date[b]Hibernate[/b][/u]" column="ddate" type="calendar_date" />
<property name="[u]beginTime[b]Hibernate[/b][/u]" column="beginTime" type="calendar" />
<property name="[u]endTime[b]Hibernate[/b][/u]" column="endTime" type="calendar" />
<bag name="spans" inverse="true">
<key column="day_id" />
<one-to-many class="com.sccc.puncher.domain.Span"/>
</bag>
</class>
...and having seperate getters and setters in my persistant classes such as...
Code:
public void setDateHibernate(GregorianCalendar date) {
this.date.clear();
this.date.setTime(date.getTime());
this.date.clearTimeFields();
}
public GregorianCalendar getDateHibernate() {
GregorianCalendar cal = (GregorianCalendar) this.date;
return cal;
}
public void setBeginTimeHibernate(GregorianCalendar time) {
this.beginTime.clear();
this.beginTime.setTime(time.getTime());
}
public GregorianCalendar getBeginTimeHibernate() {
return (GregorianCalendar) this.beginTime;
}
public void setEndTimeHibernate(GregorianCalendar time) {
this.endTime.clear();
this.endTime.setTime(time.getTime());
}
public GregorianCalendar getEndTimeHibernate() {
return (GregorianCalendar) endTime;
}
...which allows hibernate to persist the subclass as the base class, and then de-persist it into the base class (which my hibernate setters then make the translation to the subclass). This way, I can extend the behavior of standard data types without having to look at a binary field when it comes to trying to see or edit what is being stored.
What I was wondering is whether there might be a better way to do this. I ask because it seems that I am tying the entity in my domain layer to the persistence service, which seems like a no-no. Any ideas or suggestions are greatly appreciated.