-->
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.  [ 2 posts ] 
Author Message
 Post subject: Help with using a Discriminator
PostPosted: Fri Aug 22, 2008 11:21 am 
Newbie

Joined: Mon Aug 18, 2008 2:24 pm
Posts: 8
I currently have two tables and I am trying to use the discrimintor to get 3 different sets based on the animal_type_code.

Table A
id (primary key)
animal_code
animal_type_code

The hbm.xml file looks something like

<class name="Animal" table="Table A">
<id name="ID" column="Id">
<generator class="native" />
</id>
<discriminator column="animal_type_code " />
<subclass name="Dog" discriminator-value="D">
</subclass>
<subclass name="Cat" discriminator-value="C">
</subclass>
<subclass name="Pig" discriminator-value="P">
</subclass>
</class>


In my other hbm.xml file, I am trying to get a set that contain only Dogs, a set that contains only Cats, and a set that contain only Pigs. However, i don't know how to map this. I tried something similar but I noticed that you can't have a reference to the same column. This what I was trying to do but it doesn't work

<set name="dogs" table="table A" inverse="true" lazy=false>
<key column="id"/>
<many-to-many column="animal_code" class="Animal"/>
</set>

<set name="cats" table="table A" inverse="true" lazy=false>
<key column="id"/>
<many-to-many column="animal_code" class="Animal"/>
</set>

<set name="pigs" table="table A" inverse="true" lazy=false>
<key column="id"/>
<many-to-many column="animal_code" class="Animal"/>
</set>

And in the java file I have three sets defined along with getters and setters as such:
private Set<Dog> dogs;
private Set<Pig> pigs;
private Set<Cat> cats;

Can someone please give me an idea how I should probably map the files to get the results that I am trying to receive. Thanks


Top
 Profile  
 
 Post subject: Do it with HQL
PostPosted: Fri Aug 22, 2008 12:05 pm 
Newbie

Joined: Fri Aug 22, 2008 11:07 am
Posts: 6
Location: Italy
Hi,
I'm not sure that you can do that.
But you can write an HQL query to retrieve all DOGS like this:

String query ="select d from Dog d";

List l = Hibernate.getSession().createQuery(q).list();


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