-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping a single object to two tables
PostPosted: Thu May 25, 2006 11:36 pm 
Newbie

Joined: Thu May 25, 2006 7:31 am
Posts: 2
HI,
I have requirement where I have to map a single Alarm object to two tables as ActiveAlarm and HistoricalAlarms. The columns in both the table are the same. I hope it is not an association.

Can someone help me out in solving this using hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 3:27 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Its wise to have one class per table.

Since you have two tables with the same column. Possibly keep a base class as Alarm. Then create two others class for the table and extend the class Alarm.

Any comments???????????

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 27, 2006 5:01 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
Are trigger an option ? If so it shouldn't be hard to do your requirement.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 12:21 am 
Newbie

Joined: Thu May 25, 2006 7:31 am
Posts: 2
Thanks Ramnathan for your support.
I have tried the same as you suggested by I have a requirement where my activealarm has to be converted into historicalalarm and then the historicalalarm has to be inserted into the historicalalarm table using spring HibernateTemplate. When I do

public void addAlarms(Alarm alarm) {
HistoricalAlarm halarm = (HistoricalAlarm)alarm;
getHibernateTemplate().save(halarm);

}
I get a classcastexception.
Exception in thread "main" java.lang.ClassCastException: com.eci.alarm.dao.ActiveAlarm
at com.eci.alarm.dao.AlarmDaoImpl.addAlarms(AlarmDaoImpl.java:20)
at com.eci.alarm.dao.Main.main(Main.java:18)

Here the Alarm object is a superclass & activealarm and historicalalarm are the subclasses.

Only way to do this happen is that I have to manually write code to map ActiveAlarm object to HistoricalAlarm object and then save the object to database. Is there any other way to do this with manual mapping of objects.

Santhamohan


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 2:04 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

I suppose the object you pass into the method is an activeAlarm. So you need to clone the object to HistoricalAlarm and viceversa.

If you get different objects of Alarm in the runtime. Do an instanceof check to prevent from ClassCastException
i.e)
public void addAlarms(Alarm alarm) {
if(alarm instanceof HistoricalAlarm){
HistoricalAlarm halarm = (HistoricalAlarm)alarm;
getHibernateTemplate().save(halarm);
}else{
// place your activealarm here
}
}

All the Best

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 2:34 am 
Newbie

Joined: Mon May 29, 2006 12:57 am
Posts: 11
Location: india
Ramnath is correct.

U can map the active alarm object to the user data and clone the same on the server. Typecast the same to Historical alarm and then save it separately.Use deepClone() which might save u lots of coding.

Any comments???


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

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.