Table relations don't work after upgrade

I’m upgrading one of our ancient projects to Hibernate 6.0 (from 4.3), and now I’m getting UnknownTableReferenceException when I create the SessionFactory. Our mappings are in .hbm.xml files, and the relevant parts look like this:
Materials.hbm.xml:

<hibernate-mapping>
  <class abstract="true" name="AbstractMaterial" table=...>
    <id> column="MATERIAL_ID" name="id>
      <generator class="sequence">
    </id>
    ...
    <joined-subclass name="HotMetalMaterial" table=...>
      <key column="MATERIAL_ID>
      <many-to-one class="SmeltFurnace" column="FURNACE_ID" name="furnace">
      ...
    </joined-subclass>
  </class>
</hibernate-mapping>

SmeltingPlant.hbm.xml:

<hibernate-mapping>
  <class abstract=true name="GenericFurnace" table=...>
    <id column="UNIT_ID" name="id">
      <generator class="sequence">
    </id>
    ...
    <joined-subclass name="SmeltFurnace" table=...>
      <key column="UNIT_ID">
      ...
    </joined-subclass>
  </class>
</hibernate-mapping>

From what I understand, the different names used (FURNACE_ID vs UNIT_ID) should not be a problem, because Hibernate knows that FURNACE_ID is a foreign key to a SmeltingPlant. But I get

java.lang.IllegalStateException: Could not resolve table reference ...
Caused by: org.hibernate.sql.ast.tree.from.UnknownTableReferenceException: Unable to determine TableReference (SMELT_FURNACE_TABLE) for `treat(treat(SmeltFurnace as GenericFurnace).electricitySecondary.inheritance.ancestor as HotMetalMaterial).furnace.{id}`

This seems to be an very rare exception, since the only hits I get on Google for it is its listing in Hibernate API reference.

Some new information. After looking at the database, it turns out that electricitySecondary (that is, the foreign key referencing a Material) is null. This has not, however, been a problem before, and in the hbm.xml we have set

<many-to-one class=... column=... name=... not-null="false" not-found="ignore" />

so it should just smack a null into the reference field in the java class and move on, right?

Edit: I find it hard to believe it relates to the null value, since the exception occurs during SessionFactory creation, before any objects are loaded from the database.

More updates: I downgraded to 5.6 and the error disappears. The project uses Ants currently, so it might be that optional foreign keys have been split off to a separate JAR file that was not added. I will try to create a minimal failing test case over the weekend.

Hibernate 6.0 is quite new and this might be a bug. Can you share which version you used exactly? Also, would it be possible for you to share the full mapping in the form of a reproducer so that we can fix this?

Hibernate 6.0.2-Final, Struts 1.3.0, Hibernate-JPA 1.0.2.Final.

As I said above, I’ll try to reproduce it with a dummy setup over the weekend.

1 Like

OK, I’ve got a set that crashes with a NullPointerException in buildMetadata() (at least that’s the line Java blames). Where do I submit the archive (I’d prefer a non-public location although I’ve done my best to scrub it of business data)?

Hey there. You can join us on Zulip and send me the project through a PM, but if it isn’t too much trouble for you, it would be best if you could attach it on a new JIRA issue with a small explanation about what is wrong.

1 Like

I’ve made a new issue in JIRA.

The NPE seems to come in mapping.ToOne.sortProperties(), line 170, where getReferencedPropertyName() is null and entityBinding is also null (this causes the exception). I suspect it relates to the <composite-element>s in ProductionUnit#BlastFurnace (they used to have the same Java class, but making a new class did not resolve the issue). I suspect your JARs have all debug statements stripped, but is there a way to see where in the entity tree it is when the exception occurs without modifying and recompiling the source code?

1 Like

Not sure what you mean by “without modifying and recompiling”, but you can configure logging through log4j or java.util.logging means. Anyway, thanks for the reproducer. I’ll try to look into it asap