admin管理员组

文章数量:1304199

I have a common superclass marked as @MappedSuperClass and five other classes marked as @Entity that extend the superclass. Everything works fine in saving them with a table per subclass. The problem is i have another entity that contains a List of objects belonging to the five subclasses. I need to persist that list. I tried generics but i get an error from spring. Is there a solution or do i have to keep an array of IDs instead of a list?

I tried all annotations @ManyToMany etc. I tried Generics. List<? extends SuperClass> nothing seems to work. If there is no solution what are the strategies i can apply instead of a list of IDs? P.S I'm new to spring

I have a common superclass marked as @MappedSuperClass and five other classes marked as @Entity that extend the superclass. Everything works fine in saving them with a table per subclass. The problem is i have another entity that contains a List of objects belonging to the five subclasses. I need to persist that list. I tried generics but i get an error from spring. Is there a solution or do i have to keep an array of IDs instead of a list?

I tried all annotations @ManyToMany etc. I tried Generics. List<? extends SuperClass> nothing seems to work. If there is no solution what are the strategies i can apply instead of a list of IDs? P.S I'm new to spring

Share Improve this question edited Feb 4 at 21:02 Geii Lvov 2,7851 gold badge8 silver badges27 bronze badges asked Feb 4 at 19:53 Jason AlexanderJason Alexander 132 bronze badges 3
  • 1 Think about it from a table perspective - how do you expect the foreign keys to work? From a JPA perspective, there is no base entity for your ManyToMany mapping. The MappedSuperClass allows for java inheritance, but isn't entity inheritance (so it isn't really table per class JPA inheritance) – Chris Commented Feb 4 at 21:05
  • i think what i wrote about generics is wrong. But the problem remains. Class A is the superclass. Classes B,C,D are the subclasses for which i have 3 separate tables. Entity E contains List of objects of type B,C,D(how to implement that?? i think List<A> works). i have to persist those objects when persisting E. Is the only option to make a transactional query and persist each object(B,C,D) get their IDs and create a separate table with IDs and types(B,C,D) and save a List of IDs to that table along with the other fields of E? – Jason Alexander Commented Feb 4 at 21:48
  • Answer below is where I was pointing you toward. Joined inheritance is the easiest option (and arguably the best). There are ways some providers support table per class inheritance as you've setup, but it isn't overly efficient, and has gaps in the different providers. I remember it being called variable one to one in EclipseLink: eclipse.dev/eclipselink/documentation/2.4/jpa/extensions/… – Chris Commented Feb 6 at 19:48
Add a comment  | 

1 Answer 1

Reset to default 0

The issue you’re running into is because your common superclass is annotated with @MappedSuperclass. In JPA, a mapped superclass isn’t an entity—you can’t target it in relationships. That’s why having a List<? extends SuperClass> (or List) in your other entity doesn’t work as expected.

Instead of using @MappedSuperclass, you should make your common superclass an actual entity and define an inheritance strategy. For example, if you want a table-per-subclass setup, you can use the JOINED strategy. Something like this:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class SuperClass {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    // Common fields
}

Then, each of your subclasses can simply be:

@Entity
public class SubClassA extends SuperClass {
    // Fields specific to SubClassA
}

And your container entity that holds the list could look like this:

@Entity
public class ContainerEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(cascade = CascadeType.ALL)
private List<SuperClass> items;

// Other fields, getters, setters
}

With this setup, JPA will recognize the polymorphic relationship. When you persist a ContainerEntity, it will handle the associated SuperClass instances (and their concrete types) properly.

本文标签: