我正在使用Java + Spring + Hibernate
在测试以下代码时,我收到了错误消息:
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException:无法获取字段值 反射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;
...
...
..
}
将private UUID policyID;
从UUID
更改为Policy
时,它的工作正常,有没有办法在某种程度上设置对Policy内部特定对象的引用作为ID,而不是保持整体Assignment对象中的策略对象?
答案 0 :(得分:1)
可以只包含UUID,但它不再是hibernate托管的FK。我不建议您这样做,因为您希望Hibernate管理您的FK ..您只需将Assignment
更改为:
@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;
}
话虽如此,除非您手动将它们添加到数据库中,否则您将丢失所有Cascade选项和FK检查约束。
答案 1 :(得分:0)
我认为没有理由使用UUID而不是对Policy-Object的引用。 您可以注释您想要延迟加载对象,因此只有在您想要访问Policy-Object的另一个属性时才从数据库加载它:
@OneToOne(targetEntity = Policy.class,fetch = FetchType.LAZY)