当我尝试从 JPA 存储库获取实体列表时,总是会遇到这样的异常
org.springframework.orm.jpa.JpaSystemException: No default constructor for entity: : pl.hycom.hyper.hyebok.model.ServiceEntity$Id; nested exception is org.hibernate.InstantiationException: No default constructor for entity: : pl.hycom.hyper.hyebok.model.ServiceEntity$Id
我的实体中缺少什么。我在 Embeddable 类和外部类中都有非参数构造函数和全参数构造函数。我找不到这个问题的解决方案。
下面是我的实体
@Entity
@Table(name = "he_service")
public class ServiceEntity implements Serializable {
@EmbeddedId
private Id id ;
private String name;
public ServiceEntity() {
}
public ServiceEntity(Id id, String name) {
this.id = id;
this.name = name;
}
@Embeddable
class Id implements Serializable {
public Id() {
}
public Id(String serviceId, String clientId) {
this.serviceId = serviceId;
this.clientId = clientId;
}
@Column(name = "serviceId")
private String serviceId;
@Column(name = "clientId")
private String clientId;
public String getServiceId() {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
}
存储库方法有
@Query(value= "SELECT s FROM ServiceEntity s " +
"WHERE s.id.clientId = :clientId")
List<ServiceEntity> findByClientId(@Param("clientId") String clientId);
最佳答案
您的内部 Id
类是非静态的,这意味着它创建一个构造函数
class Id implements Serializable {
public Id(ServiceEntity arg0) {
}
// …
}
将其更改为静态
类
static class Id implements Serializable {
// …
}
关于java - 缺少具有 EmbeddedId 的实体的默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46910955/