# MappingException after Update from 5.2.17 to 5.4.22

**URL:** <https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717>\
**Category:** Hibernate ORM\
**Created:** [November 3, 2020, 11:46am UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717 "2020-11-03T11:46:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keoma](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Keoma](https://discourse.hibernate.org/u/Keoma)\
**Post date:** [November 3, 2020, 11:46am UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/1 "2020-11-03T11:46:03Z")

</div>

The error message is:  
`org.hibernate.MappingException: property [userId] not found on entity [CostCenter]`

This doesn’t make any sense since userId is a property of User, not CostCenter.

The mappings worked with Hibernate 5.2.17 and were not changed:

```auto
<class name="CostCenter" table="SIS_KST" select-before-update="true">
	<cache usage="read-write"/>
	<id name="code" column="KST_CD" length="6">
	  <type name="CustomDbType">
		<param name="implementorKey">1026</param>
	  </type>
	</id>
	<version name="changeId" column="KST_CH_ID" type="versiontype"/>
	...
    <set name="costCenters" inverse="true" optimistic-lock="false">
      <key column="KST_NEW_CD"/>
      <one-to-many class="CostCenter" not-found="ignore"/>
    </set>
	...
	<many-to-one name="successor" unique="false" column="KST_NEW_CD" not-null="false" class="CostCenter"/>
	<many-to-one name="supervisingUser" unique="false" column="KST_KTKUSR" not-null="false" class="User" property-ref="userId" fetch="join"/>
	...
</class>

<class name="User" table="G_USER" select-before-update="true">
	<cache usage="read-write"/>
	<id name="id" column="GU_LFDNR" length="7">
	  <type name="CustomDbType">
		<param name="implementorKey">4342</param>
	  </type>
	</id>
	...
    <set name="costCenters" inverse="true" optimistic-lock="false">
      <key column="KST_KTKUSR" property-ref="userId"/>
      <one-to-many class="CostCenter" not-found="ignore"/>
    </set>
	...
    <property name="userId" unique="true" column="GU_ID" not-null="true" length="10">
      <type name="CustomDbType">
        <param name="implementorKey">4384</param>
      </type>
    </property>
	...
</class>

```

I only removed the qualifying package names from the above xml.

Seems like a bug to me.

---

<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:** [November 4, 2020, 4:37pm UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/2 "2020-11-04T16:37:04Z")

</div>

Can you provide a reproducer for that, preferrably with the [testcase template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)?

I’m not fluent in the hbm.xml schema, but it seems to me that the following is the issue:

```
<set name="costCenters" inverse="true" optimistic-lock="false">
  <key column="KST_KTKUSR" property-ref="userId"/> <!-- THIS HERE -->
  <one-to-many class="CostCenter" not-found="ignore"/>
</set>
```

---

<div class="post-metadata">

**Author:** ![Keoma](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Keoma](https://discourse.hibernate.org/u/Keoma)\
**Post date:** [November 5, 2020, 8:41am UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/3 "2020-11-05T08:41:01Z")

</div>

That’s exactly what I thought at first. But if I remove the `property-ref`, or even the whole `<set>` mapping, the error persists. To make things even worse, if I remove the _other_ `<set>` mapping, the one in the CostCenter entity itself, the error is gone. Doesn’t make any sense whatsoever. Seriously weird.

---

<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:** [November 5, 2020, 8:54am UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/4 "2020-11-05T08:54:11Z")

</div>

I don’t know how to map this with hbm.xml but if you can switch to annotations, the following should work:

```auto
@Entity
@Table(name = "SIS_KST")
public calss CostCenter {
    @Id
    @Column(name = "KST_CD", length = 6)
    @Type(type = "CustomDbType", parameters = @Parameter(name = "implementorKey", value = "1026"))
    String code;
    @Version
    @Column(name = "KST_CH_ID", columnDefinition = "versiontype")
    Long changeId;

    @ManyToOne
    @JoinColumn(name = "KST_NEW_CD")
    CostCenter successor;
    @ManyToOne
    @JoinColumn(name = "KST_KTKUSR", referencedColumn = "GU_ID")
    User supervisingUser;

    @OneToMany(mappedBy = "successor")
    Set<CostCenter> costCenters;
}

@Entity
@Table(name = "G_USER")
public calss User {
    @Id
    @Column(name = "GU_LFDNR", length = 7)
    @Type(type = "CustomDbType", parameters = @Parameter(name = "implementorKey", value = "4342"))
    String code;

    @Column(name = "GU_ID", length = 10, unique = true)
    @Type(type = "CustomDbType", parameters = @Parameter(name = "implementorKey", value = "4384"))
    String userId;

    @OneToMany(mappedBy = "supervisingUser")
    Set<CostCenter> costCenters;
}

```

Maybe this helps you fix the hbm.xml mapping as well.

---

<div class="post-metadata">

**Author:** ![Keoma](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Keoma](https://discourse.hibernate.org/u/Keoma)\
**Post date:** [November 5, 2020, 9:32am UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/5 "2020-11-05T09:32:58Z")

</div>

Thanks for the suggestion. I am sure those annotation should work. So should the xml though. And it did, with Hibernate 5.2.

Cannot switch to annotations, all the xml is generated on system start.

---

<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:** [November 5, 2020, 2:19pm UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/6 "2020-11-05T14:19:13Z")

</div>

If you can provide a [test case](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) I can give this a deeper look

---

<div class="post-metadata">

**Author:** ![Keoma](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Keoma](https://discourse.hibernate.org/u/Keoma)\
**Post date:** [November 5, 2020, 2:48pm UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/7 "2020-11-05T14:48:41Z")

</div>

Thanks a lot for the offer. I can’t use the template though as it assumes I’m using JPA. Which I don’t. Just plain old proprietary Hibernate:

```auto
MetadataSources sources = new MetadataSources(...);
Stream<DOMDocument> docs = ...;
docs.forEach(sources::addInputStream);
Metadata metadata = sources.buildMetadata();
sessionFactory = metadata.getSessionFactoryBuilder().build();

```

But I think I found the bug anyway. Look for it in the constructor of AbstractPluralAttributeSourceImpl:

```auto
protected AbstractPluralAttributeSourceImpl(
			MappingDocument mappingDocument,
			final PluralAttributeInfo pluralAttributeJaxbMapping,
			AttributeSourceContainer container) {
		super( mappingDocument );
		this.pluralAttributeJaxbMapping = pluralAttributeJaxbMapping;
		this.container = container;

		this.attributeRole = container.getAttributeRoleBase().append( pluralAttributeJaxbMapping.getName() );
		this.attributePath = container.getAttributePathBase().append( pluralAttributeJaxbMapping.getName() );

/* This whole thing is new in 5.4 */

		Optional<JaxbHbmManyToOneType> jaxbHbmManyToOneTypeOptional = Optional.empty();

		if ( pluralAttributeJaxbMapping.isInverse() && pluralAttributeJaxbMapping.getOneToMany() != null ) {
			String childClass = pluralAttributeJaxbMapping.getOneToMany().getClazz();

			if ( childClass != null ) {
				jaxbHbmManyToOneTypeOptional = mappingDocument.getDocumentRoot().getClazz()
						.stream()

	/* If I'm not mistaken, this checks if the class in question references itself */
						.filter( (JaxbHbmRootEntityType entityType) -> childClass.equals( entityType.getName() ) )

	/* If it does, we forget about it and look for any (!!!) many-to-one attribute with a property-ref ... */
						.flatMap( jaxbHbmRootEntityType -> jaxbHbmRootEntityType.getAttributes().stream() )
						.filter(
								attribute -> attribute instanceof JaxbHbmManyToOneType &&
										( (JaxbHbmManyToOneType) attribute ).getPropertyRef() != null )
						.map( JaxbHbmManyToOneType.class::cast )
						.findFirst();
			}
		}

	/* ... and use it to fill the keySource, even if (as in my case) it has nothing to do with the original pluralAttributeJaxbMapping at all. This cannot work. */

		this.keySource = jaxbHbmManyToOneTypeOptional
				.map( jaxbHbmManyToOneType -> new PluralAttributeKeySourceImpl(
						sourceMappingDocument(),
						jaxbHbmManyToOneType,
						container

/* This is the old 5.2 behavior, which does work. */

				) ).orElseGet( () -> new PluralAttributeKeySourceImpl(
						sourceMappingDocument(),
						pluralAttributeJaxbMapping.isInverse() ? /* And what's this? It's not wrong, but still. */
								pluralAttributeJaxbMapping.getKey() :
								pluralAttributeJaxbMapping.getKey(),
						container
				) );

```

I just don’t know what is the right fix here. That will have to be figured out by someone who has an idea about why all this stuff was added in the first place.

---

<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:** [November 6, 2020, 9:22am UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/8 "2020-11-06T09:22:05Z")

</div>

There is a test case template for using Hibernate APIs as well of course 🙂

> <https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java>

Thanks for your analysis so far. We really need a test case though, so please try to take the time to write one.

---

<div class="post-metadata">

**Author:** ![Keoma](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Keoma](https://discourse.hibernate.org/u/Keoma)\
**Post date:** [November 6, 2020, 12:05pm UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/9 "2020-11-06T12:05:26Z")

</div>

How can I attach the test files?

---

<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:** [November 6, 2020, 3:59pm UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/10 "2020-11-06T15:59:13Z")

</div>

Just fork the repository on Github and send me a link to the test on your Github fork.

---

<div class="post-metadata">

**Author:** ![Keoma](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Keoma](https://discourse.hibernate.org/u/Keoma)\
**Post date:** [November 9, 2020, 7:45am UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/11 "2020-11-09T07:45:29Z")

</div>

Made a test [here](https://github.com/Keoma-github/hibernate-test-case-templates/tree/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs).

---

<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:** [November 10, 2020, 8:55pm UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/12 "2020-11-10T20:55:38Z")

</div>

Thanks, I’ll take a look as soon as I find the time

---

<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:** [November 11, 2020, 4:25pm UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/13 "2020-11-11T16:25:15Z")

</div>

It’s a bug. I created [https://hibernate.atlassian.net/browse/HHH-14322](https://hibernate.atlassian.net/browse/HHH-14322) for this

---

<div class="post-metadata">

**Author:** ![Keoma](https://avatars.discourse-cdn.com/v4/letter/k/858c86/32.png) [@Keoma](https://discourse.hibernate.org/u/Keoma)\
**Post date:** [November 11, 2020, 4:35pm UTC](https://discourse.hibernate.org/t/mappingexception-after-update-from-5-2-17-to-5-4-22/4717/14 "2020-11-11T16:35:49Z")

</div>

Thanks for your support!
