1

I have 2 entities and 1 embeddable object :

@Entity
    class CourseDetails extends Course {
    @Id
    Integer id;
    @ElementCollection
        @CollectionTable(name = "course_section", joinColumns = @JoinColumn(name = "courseId"), foreignKey = @ForeignKey(name = "course_section_fk"))
        private List<CourseSection> courseSection;
    }
    
    @Embeddable
    public class CourseSection extends BaseBo {
    @OneToMany
        @JoinColumn(name="contentId")
        private Set<CourseContent> courseContent = new HashSet<>();
    }
    
    @Entity
    public class CourseContent {
    
        private static final long serialVersionUID = 1856738483334146418L;
        @Id
        private Integer contentId;
        private String contentSummary;
    }

I want to store coursesection as an embedded object of course and course_section should contain reference of course_content. I tried the above structure but it gives error :

@ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection

How to achieve this in spring boot-jpa ?

Achyut
  • 377
  • 1
  • 3
  • 17
  • Why do you need it to be an embeddable? – Davide D'Alto May 29 '22 at 18:35
  • @DavideD'Alto I need course section to be a part of course and not as a seperate entity – Achyut May 30 '22 at 04:27
  • It just looks like you are trying to shoehorn a Many-To_Many into an element collection. – M. Deinum May 31 '22 at 10:18
  • @Achyut why? M.Deinum seems to be correct in their assumption with this. Sounds like an https://xyproblem.info/ – SirHawrk May 31 '22 at 10:56
  • @M.Deinum Could you please suggest how this relation should be between Course, Course Section and course content. Considering course section to be a composite object of course. – Achyut May 31 '22 at 13:50
  • A composite of what? There doesn't seem to be anything but a collection in there. I also don't see anything that prevents it from being an `@Entity` as well instead of an embeddable. – M. Deinum May 31 '22 at 13:54
  • @M.Deinum .Thanks, course_section can be an entity, but I think it should be a part of course only and course_section will further store only the reference of all its content – Achyut Jun 01 '22 at 04:06

0 Answers0