gpt4 book ai didi

java - 错误类异常 : Object [id=null] was not of the specified subclass

转载 作者:搜寻专家 更新时间:2023-10-30 23:01:55 25 4
gpt4 key购买 nike

我有一个像下面这样的模型:

@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

}

但是当我尝试通过与联系人交换其中一位访客来更新 visitRequest 并尝试在 CRUD 存储库上执行该方法时 visitRequestRepository.save(visitRequest);我收到此异常:

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

也许问题在于数据库中的 contact_person 和 visitor 表中使用了相同的 ID?

我该如何解决这个问题?我已经搜索了解决方案,但没有一个对我有用。

最佳答案

您可能没有在 ContactPerson 中初始化 visitRequest 引用。请看下面的配置,它适用于 Spring Boot JPA。我用过lombok生成 GetterSetter

这是一个在 Spring Boot 中实现的工作示例 https://github.com/ConsciousObserver/SpringBootJpaInheritance

@Data
@Entity(name = "request")
class VisitRequest {
@Id
@GeneratedValue
private Long id;

@OneToMany(mappedBy = "visitRequest", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private List<Visitor> visitors = new ArrayList<>();
}

@NoArgsConstructor
@Data
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
class Visitor {
@Id
@GeneratedValue
private Long id;

@ManyToOne
@JsonBackReference
private VisitRequest visitRequest;

public Visitor(VisitRequest visitRequest) {
this.visitRequest = visitRequest;
}
}

@NoArgsConstructor
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Entity
class ContactPerson extends Visitor {
private String phoneNumber;

public ContactPerson(VisitRequest visitRequest, String phoneNumber) {
super(visitRequest);
this.phoneNumber = phoneNumber;
}
}

关于java - 错误类异常 : Object [id=null] was not of the specified subclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43848721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com