I'm using Java + Spring + Hibernate
When testing the following code, I'm getting an error:
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.XXX.XXX.entity.Policy.id
@Entity
@Table(name = "assignment")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Assignment extends BaseEntity {
@Id
@Column(name = "id")
@Type(type = "pg-uuid")
@NotTracked
private UUID id;
@OneToOne(targetEntity = Policy.class)
@JoinColumn(name = "policy_id", nullable = false, referencedColumnName = "policy_id")
@OnDelete(action = OnDeleteAction.CASCADE)
@TrackedEntity(isIdentityField = true)
private UUID policyID;
}
@Entity
@Table(name = "policy")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "policy")
@Inheritance(strategy = InheritanceType.JOINED)
public class Policy extends BaseEntity {
@Id
@Column(name = "policy_id")
@Type(type = "pg-uuid")
@NotTracked
private UUID id;
...
...
..
}
When changing the private UUID policyID;
from UUID
to Policy
, its working just fine, is there a way to set somehow a reference to specific object inside Policy as ID instead of holding the whole object of policy inside the Assignment object?
It is possible to include just the UUID, but it will not be a hibernate managed FK any longer. I don't advise this as you want Hibernate to manage your FK.. You would simply change Assignment
to:
@Entity
@Table(name = "assignment")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Assignment extends BaseEntity {
@Id
@Column(name = "id")
@Type(type = "pg-uuid")
@NotTracked
private UUID id;
@Column(name="policy_id")
private UUID policyID;
}
With that being said, you will lose all Cascade options and FK check constraints against this field unless you add them to your database manually.