Spring Boot+EclipseLink REST数据接收时JPA继承问题求助
Alright, let's work through this JPA inheritance issue you're hitting in your Spring Boot REST app. First, let's make sure I've got your class hierarchy straight:
- Base class
B(presumably an entity) - Concrete subclass
CextendingB - Abstract class
DextendingB - Concrete entity
Eextending abstract classD - Entity
Awith a reference attributeattribute1of typeB, which is actually an instance ofE
Since you mentioned inheritance strategy troubles, let's break down common pitfalls and fixes tailored to your setup:
1. Validate Your JPA Inheritance Strategy Configuration
First, double-check how you've configured inheritance for your entity hierarchy—this is the root of most这类 issues. Let's cover the three standard strategies and their gotchas:
SINGLE_TABLE (Most Common for Simple Hierarchies)
If you're using this strategy (all subclasses stored in one table with a discriminator column):
- Annotate the base class
Bwith:@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "ENTITY_TYPE", discriminatorType = DiscriminatorType.STRING) public class B { // Common fields/methods } - For each concrete subclass, add a discriminator value. Even your abstract class
Dneeds a value (EclipseLink handles abstract parent subclasses fine):@Entity @DiscriminatorValue("C") public class C extends B { /* ... */ } @Entity @DiscriminatorValue("D") public abstract class D extends B { /* ... */ } @Entity @DiscriminatorValue("E") public class E extends D { /* ... */ } - Ensure your
Aentity's attribute is mapped correctly:@Entity public class A { @ManyToOne(fetch = FetchType.LAZY) // Use EAGER only if strictly necessary private B attribute1; // Other fields }
JOINED (Normalized Table Structure)
If you're using this strategy (each class has its own table, linked via foreign keys):
- Annotate
Bwith@Inheritance(strategy = InheritanceType.JOINED) - Each subclass (including
DandE) will map to its own table, with foreign keys linking back toB's table. When queryingA, use aJOIN FETCHto load the fullEinstance:SELECT a FROM A a JOIN FETCH a.attribute1 WHERE a.id = :id - In Spring Data JPA, you can wrap this in a repository method with
@Queryto force eager loading of the subclass data.
2. Fix Common REST Serialization/Deserialization Issues
Since you're using a REST interface to receive data, you might hit problems where Jackson can't map incoming JSON to the correct subclass of B. Here's how to fix that:
- Add Jackson type info annotations to your base class
Bto tell the serializer/deserializer which subclass to use:import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonSubTypes; @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "entityType") @JsonSubTypes({ @JsonSubTypes.Type(value = C.class, name = "C"), @JsonSubTypes.Type(value = E.class, name = "E") }) @Entity // ... JPA inheritance annotations public class B { /* ... */ } - When sending data from the frontend, include the
entityTypefield in your JSON payload (e.g.,"entityType": "E"for anEinstance), and Jackson will correctly deserialize it into the right subclass forA'sattribute1.
3. EclipseLink-Specific Tweaks
Since your first app uses EclipseLink, if this REST app also uses it (instead of Spring's default Hibernate), add these properties to application.properties to avoid conflicts:
spring.jpa.database-platform=org.eclipse.persistence.platform.database.PostgreSQLPlatform spring.jpa.properties.eclipse.persistence.weaving=false spring.jpa.properties.eclipse.persistence.persistence-unit.name=yourPersistenceUnitName spring.jpa.show-sql=true # Enable to debug generated SQL
4. Debugging Tips
- Turn on SQL logging to verify that JPA is querying the correct tables and including discriminator values (check the properties above for EclipseLink/Hibernate logging).
- Test persistence directly (without the REST layer): Create an
Ainstance, setattribute1to anEinstance, save it, then retrieve it to confirm the fullEdata is loaded. - Inspect your PostgreSQL tables to ensure discriminator columns exist (for
SINGLE_TABLE) or foreign key links are correct (forJOINED).
内容的提问来源于stack exchange,提问作者Burny

