-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping 1 class to 2 tables using a discriminator value
PostPosted: Wed Mar 28, 2007 3:57 pm 
Newbie

Joined: Wed Mar 28, 2007 3:48 pm
Posts: 2
Hi,

I am using Hibernate version 3.

I am trying to map a single class "Animal" into 2 tables "ExtinctAnimals" and "NonExtinctAnimals" depending on the value of the "extinct" property. If the object has a value of "true" for the extinct property, then it should be stored into the "ExtinctAnimals" table. If it has a value of "false", then it should be stored in the "NonExtinctAnimals" table.

The same should also occur while loading the object. So, it the object is loaded from the "ExtinctAnimals" table, it should automatically set the value of the "extinct" property to true.

I would like to know what kind of mappings to use for the same. I tried the following :

Code:
<class name="Animal" entity-name="ExtinctAnimals" discriminator-value="true">
  <discriminator column="extinct" />
  ...
</class>

<class name="Animal" entity-name="NonExtinctAnimals" discriminator-value="false">
  <discriminator column="extinct" />
  ...
</class>


But, Hibernate complains that the classes "ExtinctAnimals" and "NonExtinctAnimals" do not exist. Attached at the bottom is the stack trace.

What would be a good mapping for my problem?
Or are there better recommended ways of doing things?

Thanks in advance.



Java Exception :
Caused by: java.lang.ClassNotFoundException: ExtinctAnimals
at java.net.URLClassLoader.findClass(URLClassLoader.java:376)
at java.lang.ClassLoader.loadClass(ClassLoader.java:570)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:442)
at java.lang.ClassLoader.loadClass(ClassLoader.java:502)
at java.lang.Class.forName1(Native Method)
at java.lang.Class.forName(Class.java:180)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:108)
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:76)
Code:


Top
 Profile  
 
 Post subject: Mapping 1 class to 2 tables using a discriminator value
PostPosted: Thu Mar 29, 2007 7:22 am 
Newbie

Joined: Thu Mar 29, 2007 6:51 am
Posts: 3
Hi Spaceman,

With the use a discriminator, your ExtinctAnimals and NonExtinctAnimals will end up in one table:

<class name="Animal" table="Animal">
<discriminator column="extinct" />
... // declaration of properties
<subclass name="NonExtinctAnimal" discriminator-value="false">
...
</subclass>
<subclass name="ExtinctAnimal" discriminator-value="true">
...
</subclass>
</class>


but then you will have to create the subclasses NonExtinctAnimal and ExtinctAnimal.

I know it is a bit turned around, with only one table and 2 subclasses, but I think it could work for you, or is it a requirement that the animals are stored in 2 tables?

Hope to have helped,

DrAvanlch.


Top
 Profile  
 
 Post subject: Mapping 1 class to 2 tables using a discriminator value
PostPosted: Thu Mar 29, 2007 9:18 am 
Newbie

Joined: Wed Mar 28, 2007 3:48 pm
Posts: 2
Hi drAvaInch,

Thanks for your quick reply!

But, unfortunately, it is a requirement to use just 1 class and 2 tables. I am a Java-Hibernate programmer and I cannot mess with the DB Schema.

Hibernate easily maps 2 subclasses to 1 table, but I wasnt able to do it the other way round using a discriminator.

Thanks,

Spaceman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 9:23 am 
Newbie

Joined: Thu Mar 29, 2007 6:51 am
Posts: 3
Hi Spaceman,

Ok.
Let me just think that over for a few moments... :-)

Hope to get back to you,

DrAvalnch.


Top
 Profile  
 
 Post subject: Mapping 1 class to 2 tables using a discriminator value
PostPosted: Thu Mar 29, 2007 10:13 am 
Newbie

Joined: Thu Mar 29, 2007 6:51 am
Posts: 3
Hi Spaceman,

This might get you on the way (using the entity-name) :

<class name="Animal" table="ExtinctAnimals" proxy="Animal" entity-name="ExtinctedAnimals" >
...
</class>
<class name="Animal" table="NonExtinctAnimals" proxy="Animal" entity-name="NonExtinctedAnimals" >
...
</class>

But I'm not really sure how to put in the discriminator.

But since you are the Java-Hibernate programmer you can decide on the classes, no?

You then could do something like :

abstract class Animal {

}

class ExtinctAnimal extends Animal{
...
}

class NonExtinctAnimal extends Animal {
...
}

<class name="Animal" >
<id name="id" type="long" column="ANIMAL_ID">
<generator class="sequence"/>
</id> ...
<union-subclass name="ExtinctAnimal" table="ExtinctAnimal">
...
</union-subclass>
<union-subclass name="NonExtinctAnimal" table="NonExtinctAnimal">
...
</union-subclass>
</class>

Both tables should have the ANIMAL_ID as key.

Hope to have helped,

DrAvalnch.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.