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

lua*_*nti 2 java hibernate jpa spring-data

总是当我尝试从JPA Repository获取实体列表时,我得到了这样的异常

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
Run Code Online (Sandbox Code Playgroud)

我的实体缺少什么.我在Embeddable类和外部类中都有非args构造函数和all-args构造函数.我找不到解决这个问题的方法.

我的实体如下

@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;
        }


   }
Run Code Online (Sandbox Code Playgroud)

那里的存储库方法

   @Query(value= "SELECT s FROM ServiceEntity s " +
            "WHERE s.id.clientId = :clientId")
    List<ServiceEntity> findByClientId(@Param("clientId") String clientId);
Run Code Online (Sandbox Code Playgroud)

mp9*_*1de 7

你的内部Id类是非静态的,这意味着它创建了一个构造函数

class Id implements Serializable {

    public Id(ServiceEntity arg0) {
    }
    // …
}
Run Code Online (Sandbox Code Playgroud)

把它改成一个static班级

static class Id implements Serializable {
    // …
}
Run Code Online (Sandbox Code Playgroud)