Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using discriminator on base class with no subclasses

 
Bob Donnelly
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a quick question that I hope somebody can help me with...

I have a table in my DB called AGREEMENT and there are 2 different objects types that get stored in this table. The way to tell which type of object it is, is by a discriminator column called DISC. I only ever need to deal with one of these objects, so in my mapping file I want to add a discriminator so that any other classes that are mapped to this one will always ONLY instantiate those rows that contain a specific discriminator value.

This is the mapping file that I set up:

<class name="agreement"
table="AGREEMENT"
discriminator-value="AGR">

<id name="id" column="AGREEMENT_ID">
<generator class="native"/>
</id>

<discriminator column="DISC" type="string"/>

....<property>....

</class>

So I want any other class that has a reference to this one, to only instantiate the rows that have "AGR" in the DISC column. I know I could do this with sql or a where clause, but the problem is that I need to be able to also write this value to the DISC column when I create and insert new rows, so the value must be mutable.

Anybody know the right way to do this? Using the mapping above, I still get every row loaded, irrelevant of what the discriminator is.

Thanks alot for any help!

(By the way, I'm not even sure if using a discriminator element is the correct way to do this since I have no subclasses)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

(By the way, I'm not even sure if using a discriminator element is the correct way to do this since I have no subclasses)

Its not the correct way, you need to be using subclasses if you want to use the "table per class hierarchy" design. Using the discriminator-value attribute on the base class declaration is setting a default I think. You need to define a subclass for the discriminator you are interested in and query for that class. That should work.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!