Hibernate Inheritance strategy=InheritanceType.JOINED & onetoMany with spring-data-jpa

感情迁移 提交于 2019-12-25 03:32:33

问题


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 ?


回答1:


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());
        }
    }



回答2:


May be the issue is

@OneToMany(fetch = FetchType.LAZY, mappedBy = "exam") private Set object

When you fetch any thing by LAZY loading FetchType.LAZY. This will get all the object from teh parent table i.e Exam here but will not query the child/dependent tables for the data.

e.g Here it will not hit the ExamObject to get its data, it just replaces this by the proxy object, Thus if you query this object then you get null as the result.

Try your query with FetchType.EAGER



来源:https://stackoverflow.com/questions/29764533/hibernate-inheritance-strategy-inheritancetype-joined-onetomany-with-spring-da

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!