我试图用注解在jpa中Map以下案例:
数据库模型
因此,我们有一个根对象,它与我们将要保存在事务中的源有关系,但是在对该根对象进行处理之后,我们将获得结果对象
这里是我的Map:
public class Root {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="source_id", unique= false, nullable=false, insertable=true, updatable=false)
private Source source;
@Column(name = "label", nullable = true, insertable = true, updatable = false)
private String label;
}
public class Source {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "label", nullable = true, insertable = true, updatable = false)
private String label;
}
public class Result {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="source_id", unique= false, nullable=false, insertable=true, updatable=false)
private Source source;
@Column(name = "label", nullable = true, insertable = true, updatable = false)
private String label;
}
我的用例:
我保存了对象源和根mysql:
INSERT INTO source (label) VALUES ("FIRST"); -- id: 1
INSERT INTO root (source_id, label) VALUES (1, "MY FIRST");
因此,在处理之后,我想保存对象结果以将该值与源对象链接起来,但我只想链接select(而不是insert/update/remove)
我的Lombok湖代码:
SourceEntity source = SourceEntity.builder().id(root.getId()).build();
ResultEntity result = ResultEntity.builder().source(source).label("REFERENCE TO FIRST").build();
dao.save(result);
错误:
Cannot add or update a child row: a foreign key constraint fails (`mydb`.`result`, CONSTRAINT `result_fk0` FOREIGN KEY (`source_id`) REFERENCES `source` (`id`))
我知道在Map中我做错了什么,但是我找不到错误,可能是级联类型?我试过了 CascadeType.DETACH
我也是。
谢谢,
暂无答案!
目前还没有任何答案,快来回答吧!