gpt4 book ai didi

java - 缺少具有 EmbeddedId 的实体的默认构造函数

转载 作者:行者123 更新时间:2023-12-02 11:16:11 24 4
gpt4 key购买 nike

当我尝试从 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/

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