-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate - Mapping of generic class
PostPosted: Thu Aug 23, 2007 10:06 am 
Newbie

Joined: Thu Aug 23, 2007 5:33 am
Posts: 1
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.