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

标签 java hibernate jpa spring-data

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

相关文章:

Java 泛型 : Obtaining a Class<Collection<T>>?

java - Play 2.2.0 - 如何创建 war 文件

java - 我是否需要模拟服务和存储库类来测试 Controller 的简单方法?

hibernate - JPA,Hibernate 我可以做复合主键,其中一个元素是外键@OneToMany?

java - 使用 Spring Data 和 HIbernate JPA 进行延迟加载

java - 捕获 oracle 序列并将其设置到 JPA 中的另一个字段

java - SWT:拖放内部对象

java - 为什么我的控制台中会出现此错误?

java - 如何在hibernate中不添加引用@Entity的情况下触发外键关系?

java - Java中按日期字段对对象列表进行排序