luafanti
luafanti

Reputation: 411

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);

Upvotes: 3

Views: 13665

Answers (4)

Gel
Gel

Reputation: 3026

Im new to Java, and have been doing an exploration in full stack java and angular application.

I created a service that allows me to perform CRUD operations and store my data in an in-memory database.

I was trying to display my data in http://localhost:8080/users/Joe/all-todos and I get the error that the OP has raised.

To solve mine, I had to create a default constructor like so: // JPA expects a default constructor in my entity class ToDo protected ToDo() {}

Note here that I am using JPA. I just add that default constructor to my entity class and run my java application and it rendered the data.

Hope this helps in some way with regards to the error message.

Upvotes: 0

pirho
pirho

Reputation: 12215

You can also declare Id in file of its own Id.java like class ID { } or public class ID { } if need to keep in different package.

Related information in general: How can I resolve “an enclosing instance that contains X.Y is required”?

Maybe the problem is that Spring fails on this

an enclosing instance that contains ServiceEntity.ID is required

Upvotes: 0

mp911de
mp911de

Reputation: 18119

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 {
    // …
}

Upvotes: 10

xiaofeng.li
xiaofeng.li

Reputation: 8587

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

Upvotes: 2

Related Questions