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
- 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
1 Answer
Reset to default 0The 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.
本文标签:
版权声明:本文标题:java - How to save List of objects that have a common superclass in Spring JPA inside another entity - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741748802a2395698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论