# Table relations don't work after upgrade

**URL:** <https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400>\
**Category:** Hibernate ORM\
**Created:** [June 15, 2022, 12:44pm UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400 "2022-06-15T12:44:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaleth81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/zaleth81/32/1999_2.png) [@zaleth81](https://discourse.hibernate.org/u/zaleth81)\
**Post date:** [June 15, 2022, 12:44pm UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/1 "2022-06-15T12:44:21Z")

</div>

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:

```auto
<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:

```auto
<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

```auto
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.

---

<div class="post-metadata">

**Author:** ![zaleth81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/zaleth81/32/1999_2.png) [@zaleth81](https://discourse.hibernate.org/u/zaleth81)\
**Post date:** [June 17, 2022, 8:21am UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/2 "2022-06-17T08:21:50Z")

</div>

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

```auto
<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.

---

<div class="post-metadata">

**Author:** ![zaleth81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/zaleth81/32/1999_2.png) [@zaleth81](https://discourse.hibernate.org/u/zaleth81)\
**Post date:** [June 17, 2022, 9:44am UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/3 "2022-06-17T09:44:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 17, 2022, 11:14am UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/4 "2022-06-17T11:14:50Z")

</div>

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](https://github.com/hibernate/hibernate-test-case-templates/blob/main/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) so that we can fix this?

---

<div class="post-metadata">

**Author:** ![zaleth81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/zaleth81/32/1999_2.png) [@zaleth81](https://discourse.hibernate.org/u/zaleth81)\
**Post date:** [June 17, 2022, 1:22pm UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/5 "2022-06-17T13:22:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zaleth81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/zaleth81/32/1999_2.png) [@zaleth81](https://discourse.hibernate.org/u/zaleth81)\
**Post date:** [June 20, 2022, 4:36pm UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/6 "2022-06-20T16:36:32Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 21, 2022, 9:56am UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/7 "2022-06-21T09:56:13Z")

</div>

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](https://hibernate.atlassian.net/jira/your-work) with a small explanation about what is wrong.

---

<div class="post-metadata">

**Author:** ![zaleth81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/zaleth81/32/1999_2.png) [@zaleth81](https://discourse.hibernate.org/u/zaleth81)\
**Post date:** [June 21, 2022, 12:19pm UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/8 "2022-06-21T12:19:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 21, 2022, 1:06pm UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/9 "2022-06-21T13:06:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nehathinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/nehathinks/32/3453_2.png) [@nehathinks](https://discourse.hibernate.org/u/nehathinks)\
**Post date:** [September 5, 2024, 6:30am UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/10 "2024-09-05T06:30:04Z")

</div>

Hey @zaleth81 , I am actually facing the exact same error and its troubling me since last 2 days, can you please tell me what did you do to solve the error in hibernate 6 when creating the sessionfactory?

---

<div class="post-metadata">

**Author:** ![zaleth81](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/zaleth81/32/1999_2.png) [@zaleth81](https://discourse.hibernate.org/u/zaleth81)\
**Post date:** [September 6, 2024, 12:48pm UTC](https://discourse.hibernate.org/t/table-relations-dont-work-after-upgrade/6400/11 "2024-09-06T12:48:47Z")

</div>

Sorry, we’re still using older versions. Hopefully after next release they will let me get back to pushing Hibernate 6 (but first we should move from Struts to Spring …)
