Hello All,
I am using java 1.5 generics in my java classes. Is it possible to map the following classes to a database using Hibernate?
Timeseries: This class represents a time series and consists of a set of timeseries elements.
Code:
public class Timeseries<T> {
private HashSet<TimeseriesElement<T>> timeseriesElemenets = new HashSet<TimeseriesElement<T>>();
public HashSet<TimeseriesElement<T>> getTimeseriesElements() {
return timeseriesElemenets;
}
public void setTimeSeriesElements(HashSet<TimeseriesElement<T>> timeseriesElements) {
this.timeseriesElemenets = timeseriesElements;
}
}
TimeseriesElement: This class stores a date and a business object.
Code:
public class TimeseriesElement<T> {
private T t;
private Date date;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
OilUsage: This is an example for a business object.
Code:
public class OilUsage {
private int amount;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
Test: This Class generates a time series with OilUsage Objects.
Code:
public class Test {
public static void main(String[] args) {
OilUsage oilusage1 = new OilUsage();
oilusage1.setAmount(100);
OilUsage oilusage2 = new OilUsage();
oilusage2.setAmount(200);
OilUsage oilusage3 = new OilUsage();
oilusage3.setAmount(50);
TimeseriesElement<OilUsage> timeserieselement1 = new TimeseriesElement<OilUsage>();
timeserieselement1.setT(oilusage1);
TimeseriesElement<OilUsage> timeserieselement2 = new TimeseriesElement<OilUsage>();
timeserieselement2.setT(oilusage2);
TimeseriesElement<OilUsage> timeserieselement3 = new TimeseriesElement<OilUsage>();
timeserieselement3.setT(oilusage3);
timeserieselement1.setDate(new Date());
timeserieselement2.setDate(new Date());
timeserieselement3.setDate(new Date());
Timeseries<OilUsage> timeseries = new Timeseries<OilUsage>();
timeseries.getTimeseriesElements().add(timeserieselement1);
timeseries.getTimeseriesElements().add(timeserieselement2);
timeseries.getTimeseriesElements().add(timeserieselement3);
}
}
I would like to know, whether Hibernate supports this kind of generic objects and sets. If so, what would the mapping
files look like? How is hibernate able to map the TimeseriesElement class, as it does not know what kind ob object the parameter T will be? When creating a mapping file for the TimeseriesElement class, I need to provide a mapping for the type T, right?
Thanks for your help!