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:
{code}
@Entity @Table(name = "USERS") public class User {
@Id @GeneratedValue protected Long id;
@NotNull @Column(unique = true) protected String customerNr;
} {code}
{code} @Entity public class Item {
@Id @GeneratedValue protected Long id;
@NotNull @ManyToOne @JoinColumn(name = "SELLER_CUSTOMERNR", referencedColumnName = "CUSTOMERNR") protected User seller; } {code}
Throws an exception when you query Items:
{code} 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) {code}
If you remove the "referencedColumnName" attribute, the binding works.
Note that this is one of those rare use cases for "property-ref" in XML binding.
|