子实体的外键在保存时未添加到父实体,一对一
Foreign key of child not added to parent entity when saving, one-to-one
我有以下设置:
- Micronaut 3.x
- Java 15
我正在尝试更新的实体:
@Setter
@Getter
@Entity
@ToString
@Table(name = "single_choice_question")
public class SingleChoiceQuestion extends OrderedQuestion {
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
private SingleAnswer answer;
@Override
public void answer(Answer answer) {
if (answer instanceof SingleAnswer single) {
this.answer = single;
}
throw new IllegalArgumentException("answer is not of SingleAnswer type");
}
}
它的子实体我试图作为上述实体的一部分保留:
@Getter
@Setter
@Entity
@Table(name = "single_answer")
public class SingleAnswer extends Answer {
@OneToOne private AnswerOption choice;
}
所有实体都继承自此:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Column(updatable = false, nullable = false, length = 36)
@Type(type = "pkg.UuidUserType") // Maps UUID to SQL VARCHAR.
private UUID id;
@Version
@Column(nullable = false)
private Integer version;
...
}
基本答案class:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Answer extends BaseEntity {
@Column(nullable = false)
private Boolean correct;
}
执行了以下几行:
@ReadOnly
public class MyService {
@Transactional
public <A extends AnswerDto> A answer(A dto, UUID question) {
var answer = questionsMapper.toAnswer(dto);
answer.setCorrect(isCorrect(dto, question));
var orderedQuestion =
orderedQuestionRepository
.findById(question)
.orElseThrow(() -> new NotFoundException("question", question));
orderedQuestion.answer(answer, false);
orderedQuestionRepository.saveAndFlush(orderedQuestion);
return dto;
}
}
预期行为:
SingleAnswer 实例被持久化,它的 FK 被保存在问题的 answer_id
列中。
实际行为:
SingleAnswer 实例已保留,但其 FK 未保存在问题的 answer_id
列中。它们没有以任何方式连接,因此孤立删除似乎也不起作用。
检查hibernate的日志后,我可以看到它只执行insert
而没有执行update
问题。
另一个观察结果是,当我删除冲洗时,孤儿删除确实有效 - SingleAnswer
不会持续存在,尽管 FK 情况仍未解决。
orderedQuestion.answer(answer);
orderedQuestionRepository.save(orderedQuestion);
我看不出这个非常基本的设置有什么问题,我们将不胜感激。
在 SingleChoiceQuestion
实体中包含指示外键的 @JoinColumn
注释:
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "answer_id")
private SingleAnswer answer;
这个问题非常简单,@ReadOnly
某个时候有人偷偷进入 class 级别,我没有注意到
我有以下设置:
- Micronaut 3.x
- Java 15
我正在尝试更新的实体:
@Setter
@Getter
@Entity
@ToString
@Table(name = "single_choice_question")
public class SingleChoiceQuestion extends OrderedQuestion {
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
private SingleAnswer answer;
@Override
public void answer(Answer answer) {
if (answer instanceof SingleAnswer single) {
this.answer = single;
}
throw new IllegalArgumentException("answer is not of SingleAnswer type");
}
}
它的子实体我试图作为上述实体的一部分保留:
@Getter
@Setter
@Entity
@Table(name = "single_answer")
public class SingleAnswer extends Answer {
@OneToOne private AnswerOption choice;
}
所有实体都继承自此:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@Column(updatable = false, nullable = false, length = 36)
@Type(type = "pkg.UuidUserType") // Maps UUID to SQL VARCHAR.
private UUID id;
@Version
@Column(nullable = false)
private Integer version;
...
}
基本答案class:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Answer extends BaseEntity {
@Column(nullable = false)
private Boolean correct;
}
执行了以下几行:
@ReadOnly
public class MyService {
@Transactional
public <A extends AnswerDto> A answer(A dto, UUID question) {
var answer = questionsMapper.toAnswer(dto);
answer.setCorrect(isCorrect(dto, question));
var orderedQuestion =
orderedQuestionRepository
.findById(question)
.orElseThrow(() -> new NotFoundException("question", question));
orderedQuestion.answer(answer, false);
orderedQuestionRepository.saveAndFlush(orderedQuestion);
return dto;
}
}
预期行为:
SingleAnswer 实例被持久化,它的 FK 被保存在问题的 answer_id
列中。
实际行为:
SingleAnswer 实例已保留,但其 FK 未保存在问题的 answer_id
列中。它们没有以任何方式连接,因此孤立删除似乎也不起作用。
检查hibernate的日志后,我可以看到它只执行insert
而没有执行update
问题。
另一个观察结果是,当我删除冲洗时,孤儿删除确实有效 - SingleAnswer
不会持续存在,尽管 FK 情况仍未解决。
orderedQuestion.answer(answer);
orderedQuestionRepository.save(orderedQuestion);
我看不出这个非常基本的设置有什么问题,我们将不胜感激。
在 SingleChoiceQuestion
实体中包含指示外键的 @JoinColumn
注释:
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "answer_id")
private SingleAnswer answer;
这个问题非常简单,@ReadOnly
某个时候有人偷偷进入 class 级别,我没有注意到