| I mapped two entities using hbm.xml mapping. Both are mapped to the same table, parameter table but they are using a discriminator value. In one of them, event entity, is mapped with natural-id element.
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/orm/hbm" package="com.personal.hibernate.model">
<class name="Event" table="Parameter" discriminator-value="EVENT-TYPE">
<id name="id" type="java.lang.String">
<column name="id"/>
<generator class="identity"/>
</id>
<discriminator column="type" force="true"/>
<natural-id mutable="true">
<property name="code" column="code" type="java.lang.String"/>
</natural-id>
<property name="name" column="name" type="java.lang.String"/>
</class>
</hibernate-mapping>
The problem is searching byNaturalId because the result is null.
Event event = session.byNaturalId(Event.class)
.using("code", "1")
.load();
In the test that I wrote, I have inserted elements (categories and events) before execute the search. The query that is failing is when it tries to find the id of the element because hibernate is not using the discriminator value:
select
event_.id as id1_0_
from
parameter event_
where
event_.code=?
I wrote a unit test here: https://github.com/jmgoyesc/discriminator-naturalid/ Please check that project to get more information about the issue because I have written a long explanation there. I hope the information will be useful to analyze the problem. Let me know if I should provide more details. I belive the failure is here: org.hibernate.internal.SessionImpl.NaturalIdLoadAccessImpl#load |