Spring数据jpa持久化嵌套更改
我有三门课: 记录/配置文件/选项Spring数据jpa持久化嵌套更改,spring,jpa,spring-data-jpa,Spring,Jpa,Spring Data Jpa,我有三门课: 记录/配置文件/选项 @Entity @Table(name="Record") public class Record implements Serializable { @Id @GeneratedValue private int id; @ManyToOne(cascade=CascadeType.MERGE) @JoinColumn(name="ProfileId") private Profile profile;
@Entity
@Table(name="Record")
public class Record implements Serializable {
@Id
@GeneratedValue
private int id;
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="ProfileId")
private Profile profile;
....
}
@Entity
@Table(name="Profile")
public class Profile implements Serializable {
@Id
@GeneratedValue
private int id;
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="OptionId")
private Option option;
....
}
@Entity
@Table(name="Option")
public class Option implements Serializable {
@Id
@GeneratedValue
private int id;
private String name;
....
}
假设原始选项为“50M”,然后我将record1.profile1.option更改为“10M”
同样,当我执行record1.setId(null)时;recordRepository.save(record1)代码>
我想从record1创建一个新条目(作为更改历史记录)
在这种情况下,因为该选项是嵌套的,所以级联类型的合并不会持久化概要文件中发生的更改。因此,当我取回记录时,它仍然会说recordNew.profile1.option
为50米
但是如果我在记录类中将cascadeType更改为cascadeType.ALL
或cascadeType.PERSIST
,当我试图保存新条目时,Spring Data JPA似乎会抱怨分离属性:org.hibernate.persistentObject异常:分离实体传递给PERSIST:com.test.lalala.profile
有没有办法解决这个问题?请在帖子中显示实体的@Id
属性。请在帖子中显示实体的@Id
属性。