Consider a Transport table with 3 fields: Engine Wheels Wings
If Engine and Wheels are non-null and Wings is null then I wish to map this to a Vehicle class
If Engine and Wings are non-null and Wheels is null then I wish to map this to a Plane class
I have been reading about discriminators to differentiate between subclasses but in any of the examples that I’ve read, they always refer to a discriminator value. Therefore, I’m wondering how I can go about my above scenario?
Do I just implement separate mappings? I was hoping to find a neater way.
Thanks
Edit
Have just looked through the reference guide again and have noticed that null and not-null are valid values. So looks like this solves my problem. Will leave this open for a little while to confirm. Thanks!
You can create an abstract parent class for your Plane and Vehicle classes and use
@DiscriminatorFormula, which you use to write a formula that generates a value that you can later annotate as the@DiscriminatorValuefor the Plane and Vehicle subclasses.Take a look at this example in the Hibernate docs (though this example uses XML for the mapping, it’s basically the same thing for the annotation):
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/example-mappings.html#example-mappings-content-discrimination