Hibernate继承策略= InheritanceType.JOINED& onetoMany与spring-data-jpa [英] Hibernate Inheritance strategy=InheritanceType.JOINED & onetoMany with spring-data-jpa
问题描述
出于某些原因,我无法获得Hibernate Inheritance strategy = InheritanceType.JOINED& onetoMany工作。
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn (name =OBJECT_TYPE)
public abstract class ExamObject {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =examid,nullable = false)
私人考试考试;
}
@Entity
@ DiscriminatorValue(Q)
公共类ExamQuestion延伸ExamObject {
私人整数questionNumber;
private String questionDesc;
}
@Entity
公共课考试{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer examid;
private String examName;
@OneToMany(fetch = FetchType.LAZY,mappedBy =exam)
private Set< ExamObject>对象
}
我的Spring Boot启动班
@SpringBootApplication
公共类ExamApp实现CommandLineRunner {
@Autowired
私人ExamQuestionRepository examQuestionRepository;
@Autowired
专用ExamRepository examRepository;
public static void main(String [] args){
SpringApplication.run(ExamApp.class,args);
$ b @Override
@Transactional
public void run(String ... arg0)抛出异常{
考试=新增考试();
exam.setExamName(Exam1);
examRepository.save(考试);
String [] questions = new String [] {Question1,Question2};
ArrayList< ExamQuestion> examQuestions =新的ArrayList< ExamQuestion();
int index = 0;
for(String questionNoDesc:questions){
index ++;
ExamQuestion examQuestion = new ExamQuestion();
examQuestion.setQuestionDesc(questionNoDesc);
examQuestion.setQuestionNumber(index);
examQuestion.setExam(考试);
examQuestions.add(examQuestion);
}
examQuestionRepository.save(examQuestions);
Iterable< Exam> examGet = examRepository.findAll();
for(Exam exam2:examGet){
System.out.println(Exam question is ..+ exam2.getObjects());
$ / code $ / pre
问题是,只要我打印考题是..+ exam2.getObjects()
,我总是得到空。我怎样才能使这个工作?
解决方案正如在原始问题的评论中解释的那样,问题在于对象图没有得到适当的维护。以下函数的一行代码修复了这个问题。 exam.setObjects(examQuestions);
已被添加
@Override
@Transactional
public void run(String ... arg0)抛出异常{
考试=新考试();
exam.setExamName(Exam1);
examRepository.save(考试);
String [] questions = new String [] {Question1,Question2};
ArrayList< ExamQuestion> examQuestions =新的ArrayList< ExamQuestion();
int index = 0;
for(String questionNoDesc:questions){
index ++;
ExamQuestion examQuestion = new ExamQuestion();
examQuestion.setQuestionDesc(questionNoDesc);
examQuestion.setQuestionNumber(index);
examQuestion.setExam(考试);
examQuestions.add(examQuestion);
}
examQuestionRepository.save(examQuestions);
exam.setObjects(examQuestions);
Iterable< Exam> examGet = examRepository.findAll();
for(Exam exam2:examGet){
System.out.println(Exam question is ..+ exam2.getObjects());
}
}
For Some reason, I am not able to get the combination of Hibernate Inheritance strategy=InheritanceType.JOINED & onetoMany working. Following are the entities.
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="OBJECT_TYPE")
public abstract class ExamObject {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "examid", nullable = false)
private Exam exam;
}
@Entity
@DiscriminatorValue("Q")
public class ExamQuestion extends ExamObject{
private Integer questionNumber;
private String questionDesc;
}
@Entity
public class Exam {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer examid;
private String examName;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "exam")
private Set<ExamObject> object
}
My Spring Boot start up class
@SpringBootApplication
public class ExamApp implements CommandLineRunner {
@Autowired
private ExamQuestionRepository examQuestionRepository;
@Autowired
private ExamRepository examRepository;
public static void main(String[] args) {
SpringApplication.run(ExamApp.class, args);
}
@Override
@Transactional
public void run(String... arg0) throws Exception {
Exam exam = new Exam();
exam.setExamName("Exam1");
examRepository.save(exam);
String[] questions = new String[]{"Question1,Question2"};
ArrayList<ExamQuestion> examQuestions = new ArrayList<ExamQuestion();
int index = 0;
for(String questionNoDesc: questions){
index++;
ExamQuestion examQuestion = new ExamQuestion();
examQuestion.setQuestionDesc(questionNoDesc);
examQuestion.setQuestionNumber(index);
examQuestion.setExam(exam);
examQuestions.add(examQuestion);
}
examQuestionRepository.save(examQuestions);
Iterable<Exam> examGet = examRepository.findAll();
for (Exam exam2: examGet) {
System.out.println("Exam question is .. " +exam2.getObjects());
}
}
}
The problems is that whenever I print "Exam question is .. "+exam2.getObjects()
, I always get null. How can I get this to work ?
解决方案 As explained in the comment in the original question, the problem is that the object graph is not being maintained properly. One extra line of code to the following function fixed the issue. exam.setObjects(examQuestions);
has been added
@Override
@Transactional
public void run(String... arg0) throws Exception {
Exam exam = new Exam();
exam.setExamName("Exam1");
examRepository.save(exam);
String[] questions = new String[]{"Question1,Question2"};
ArrayList<ExamQuestion> examQuestions = new ArrayList<ExamQuestion();
int index = 0;
for(String questionNoDesc: questions){
index++;
ExamQuestion examQuestion = new ExamQuestion();
examQuestion.setQuestionDesc(questionNoDesc);
examQuestion.setQuestionNumber(index);
examQuestion.setExam(exam);
examQuestions.add(examQuestion);
}
examQuestionRepository.save(examQuestions);
exam.setObjects(examQuestions);
Iterable<Exam> examGet = examRepository.findAll();
for (Exam exam2: examGet) {
System.out.println("Exam question is .. " +exam2.getObjects());
}
}
这篇关于Hibernate继承策略= InheritanceType.JOINED& onetoMany与spring-data-jpa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!