How to use @MappedSuperClass as @OneToMany mapping collection in JPA Entity

Viewed 428

I am using Entity Inheritance for the first time and stuck with using a MappedSuperClass as collection inside another MappedSuperClass.

I have two similar set of Entities which must save data to different tables in DB. But since the business logic is perfectly same, so I want to use base classes for these entities, so these base classes can be used in common JPA repository (@NoRepositoryBean).

@MappedSuperClass
class Classroom {

    String std;
}

@MappedSuperClass
class School {

    String name;

    @OneToMany //ERROR
    Set<Classroom> rooms; 
}

@entity
@table("A_SCHOOL")
class ASchool extends School {

    Set<AClassroom> rooms; 
}

@entity
@table("A_CLASSROOM")
class AClassroom extends Classroom {

}

@entity
@table("B_SCHOOL")
class BSchool extends School {

    Set<BClassroom> rooms; 
}

@entity
@table("B_CLASSROOM")
class BClassroom extends Classroom {

}

The issue is the @OneToMany mapping in School is not allowed since Classroom is not an entity. Considering the common JPA repository (and all other business logic) is designed to work with only School/Classroom, how should I update the association of Classrooms in School?

1 Answers
Related