Issue
- DB: H2
- Spring, JPA
I have an entity called Message with a field which type is basically just a generic parent with an id:
public class Message {
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval = true)
@NonNull
List<GenericDTO> records;
}
GenericDTO:
@Entity
@Inheritance
public class GenericDTO {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@JsonIgnore
private int dto_id;
public String toStringMessage(List<GenericDTO> records){
return "";
}
}
ChildDTO:
@Data
@Builder(toBuilder = true)
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class ChildDTO extends GenericDTO{
// some fields
}
Now when I try to save my Message, the records field is saved as GenericDTO instead of ChildDTO.
Is this issue related to H2?
Solution
The solution was a bit tricky, GenericDTO
and ChildDTO
are in different projects and what happened is that the package in the project where ChildDTO present was not scanned so it was unknown for spring. Adding a package scan solved my issue.
Answered By - gabtotal
Answer Checked By - Terry (JavaFixing Volunteer)