-->
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: Extend Simple Datatype Class Behavior
PostPosted: Tue Apr 26, 2005 2:30 pm 
Newbie

Joined: Tue Apr 26, 2005 11:59 am
Posts: 1
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.


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.