I have a Spring repository as follows:
import org.springframework.data.repository.Repository;
import org.springframework.stereotype.Component;
import com.test.domain.My;
@Component
public interface MyRepository extends Repository<My, String> {
My findOne(String code);
My findByName(String name);
}
The entity class is:
import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown=true)
@Entity
@Table(name="vMy", schema="test")
@SqlResultSetMapping(
name="something",
classes = {
@ConstructorResult(targetClass = My.class,
columns={
@ColumnResult(name = "myCode", type = String.class),
@ColumnResult(name = "myShortName", type = String.class)
}
)
}
)
public class My {
@Id
@Column(name = "myCode")
private final String code;
@Column(name = "myShortName")
private final String name;
public My(String code, String name) {
this.code = code;
this.name = name;
}
@JsonCreator()
public My(@JsonProperty("My_c") String code) {
this.code = code;
this.name = null;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "{code: " + code + ", name: " + name + "}";
}
}
When findOne or findByName is invoked, the following error is given:
org.hibernate.InstantiationException: No default constructor for entity
How can I use Spring JPA repository and not have a default constructor? I would like to keep the instance fields, code and name, final.