我有兩個實體有Id的註釋但這些Id不是表中的主鍵。我現在仍然將PK映射到實體以限制變更的初始影響。但是,使用PK的到多對多的關係關聯的關聯表拋出以下錯誤:休眠關聯表
java.lang.IllegalArgumentException: Provided id of the wrong type for class. Expected: class java.util.UUID, got class java.lang.Long
實體@Id是UUID但該表PK這是一個長期被映射爲@JoinColumn
用於關聯實體
@Embeddable
public class CustomerAccountId implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "user_id", nullable = false)
private Long customerId;
@Column(name = "account_id", nullable = false)
private Long accountId;
複合關鍵的關聯實體:
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "customerId", column = @Column(name = "user_id", nullable = false)),
@AttributeOverride(name = "accountId", column = @Column(name = "account_id", nullable = false))
})
private CustomerAccountId id;
@ManyToOne
@JoinColumn(name = "user_id", insertable = false, updatable = false, referencedColumnName = "user_id")
private Customer customer;
@ManyToOne
@JoinColumn(name = "account_id", insertable = false, updatable = false, referencedColumnName = "id")
private Account account;
發生故障的實體:
@Column(name = "user_id")
private Long serialId; // also the primary key in the table
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@org.hibernate.annotations.Type(type="pg-uuid")
@Column(name = "uid", updatable = false)
private UUID id;
有誰知道這是否可能?或者當我推動這個改變時,我將被迫更新關聯表中的內容?
我不想暴露串行ID列,因爲這是一個安全風險,並且爲了避免使用UUID作爲PK的性能打擊,我想保留串行ID作爲PK。串行標識仍然映射到實體,所以我可以分階段完成這項工作,但我不希望它是實體標識。 – peekay
如果我正確理解,你不想讓id修改? 請參閱http://stackoverflow.com/a/2217064/2710704 – Tokazio
我最初在各處都使用順序標識。現在,在進行安全審計後,我將轉換爲UUID,但由於潛在的性能問題,我不想將UUID用作PK。 – peekay