I am trying to map an existing database with a discriminator and id column. However, certain values for the discriminator column have null or empty string - basically there is nothing to look up for certain discriminator values.
Is there a way for me to tell hibernate to return NULL for an @Any property when there is an unknown discriminator values?
Example:
TABLE_WAYPOINT
TABLE_BEACON
TABLE_SEGMENT
SEGID |
FIX_TYPE |
FIX_ID |
SEG_1 |
WAYPOINT |
WP_1 |
SEG_2 |
BEACON |
BC_1 |
SEG_3 |
NOTMAPPED |
|
...
class Segment {
@Any
@AnyDiscriminator(DiscriminatorType.STRING)
@AnyKeyJavaClass(String.class)
@JoinColumn(name="fix_id", nullable=true)
@Column(name="fix_type", nullable=true)
@AnyDiscriminatorValues({
@AnyDiscriminatorValue(discriminator = "WAYPOINT", entity = Waypoint.class),
@AnyDiscriminatorValue(discriminator = "BEACON", entity = Beacon.class)
// IGNORE EVERYTHING ELSE!!!
})
Cartographic fixPoint; // should read the lat/lon of the fix type, or be null if an invalid fix type
}
I was able to hack it so that it “works”…
// Cannot use @mappedsuperclass or it throws errors
class Cartographic {
Double lat;
Double lon;
}
class Beacon extends Cartographic {
// have to re-declare these, or they return null
Double lat;
Double lon;
}
class Waypoint {/*same...*/}
class Segment {
@Any(fetch=FetchType.LAZY, optional=true) // only by setting both lazy and optional does it do the lookup without errors.
@AnyDiscriminator(DiscriminatorType.STRING)
@AnyKeyJavaClass(String.class)
@JoinColumn(name="fix_id", nullable=true)
@Column(name="fix_type", nullable=true)
@AnyDiscriminatorValues({
@AnyDiscriminatorValue(discriminator = "WAYPOINT", entity = Waypoint.class),
@AnyDiscriminatorValue(discriminator = "BEACON", entity = Beacon.class)
@AnyDiscriminatorValue(discriminator = "NOTMAPPED", entity = BogusEntity.class) // still have to have an entity for the discriminator value, but it can be bogus
})
Cartographic fixPoint; // should read the lat/lon of the fix type, or be null if an invalid fix type
}
@Jesse_Shaffer glad you were able to work around it, there is no option to default to null
when not matching any discriminators I’m afraid. This could be a new feature request, feel free to open one at our issue tracker.
We have something similar in org.hibernate.annotations.AnyDiscriminatorImplicitValues
+ a custom org.hibernate.metamodel.spi.ImplicitDiscriminatorStrategy
, but still I believe you will be required to always specify an EntityMappingType
(though that could be your “wrapper” entity for unknown values).