I have a model like this one below:
@Entity(name = "request")
public class VisitRequest {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy = "visitRequest", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private List<Visitor> visitors;
//default constructor, getters and setters
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
public class Visitor {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JsonBackReference
private VisitRequest visitRequest;
//default constructor, getters and setters
}
@Entity
public class ContactPerson extends Visitor {
private PhoneNumber phoneNumber;
//default constructor, getters and setters
}
But when I try to update a visitRequest by exchanging one of the visitors with a contact person, and try to execute the method on a CRUD repository visitRequestRepository.save(visitRequest); I'm getting this exception:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.ObjectRetrievalFailureException: Object [id=null] was not of the specified subclass [cern.ais.visits.core.domain.visitor.Visitor] : class of the given object did not match class of persistent copy; nested exception is org.hibernate.WrongClassException: Object [id=null] was not of the specified subclass [cern.ais.visits.core.domain.visitor.Visitor] : class of the given object did not match class of persistent copy] with root cause
Maybe the problem is that in the database there is the same id used in the contact_person and visitor tables?
How can I solve the problem? I've searched for the solutions but none worked for me.