We're updating the issue view to help you get more done. 

Wrong binding of @ManyToOne for non-PK associations

Description

This one is quite obscure, the problem is most likely in BinderHelper#createSyntheticPropertyReference().

A many-to-one foreign key association that doesn't reference a primary key, but a unique key:

1 2 3 4 5 6 7 8 9 10 11 12 13 @Entity @Table(name = "USERS") public class User { @Id @GeneratedValue protected Long id; @NotNull @Column(unique = true) protected String customerNr; }
1 2 3 4 5 6 7 8 9 10 11 12 13 @Entity public class Item { @Id @GeneratedValue protected Long id; @NotNull @ManyToOne @JoinColumn(name = "SELLER_CUSTOMERNR", referencedColumnName = "CUSTOMERNR") protected User seller; }

Throws an exception when you query Items:

1 2 3 4 java.lang.ClassCastException: org.jpwh.model.complexschemas.naturalforeignkey.User cannot be cast to java.io.Serializable at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:157) at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2807) at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1545)

Note that this is one of those rare use cases for "property-ref" in XML binding.

Environment

None

Status

Assignee

Unassigned

Reporter

Christian Bauer

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

5.2.10
5.0.6
5.0.7
4.1.7

Priority

Minor