1.問題描述:
有時候在 Entity 中join 另一個 Entity例如:
public class Animal extends DTO{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, updatable = false, unique = true)
private Long id;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = People.class)
@JoinColumn(name = "id", referencedColumnName = "id", nullable = true, insertable = false, updatable = false)
private People people;
}
public class People{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, updatable = false, unique = true)
}
運行的時候會報錯
cannot be cast to java.io.Serializable
2.解決在 Entity中加入 implements Serializable
public class People implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, updatable = false, unique = true)
}
原因:Hibernate 有二級緩存, 緩存會將對象寫進硬盤。就必須序列化。以及兼容對象在網絡鐘的傳輸。