Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Miss default constructor for entity with EmbeddedId

Always when i try to get list of entities from JPA Repository i got exception like this

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

What is missing in my entity. I have both non-args constructor and all-args constructor in Embeddable class and outer class. I can't find a solution for this issue.

My entity below

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


   }

Repository method there

   @Query(value= "SELECT s FROM ServiceEntity s " +
            "WHERE s.id.clientId = :clientId")
    List<ServiceEntity> findByClientId(@Param("clientId") String clientId);
like image 897
luafanti Avatar asked Jul 21 '26 03:07

luafanti


2 Answers

Your inner Id class is non-static which means it creates a constructor

class Id implements Serializable {

    public Id(ServiceEntity arg0) {
    }
    // …
}

Change it to a static class

static class Id implements Serializable {
    // …
}
like image 137
mp911de Avatar answered Jul 23 '26 20:07

mp911de


Make the Id class static. Otherwise it'll require an instance of ServiceEntity to instantiate.

like image 34
xiaofeng.li Avatar answered Jul 23 '26 21:07

xiaofeng.li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!