如何在Spring JPA存储库中使用构造函数映射 [英] How to use Constructor Mapping with Spring JPA Repositories
问题描述
我有一个Spring存储库,如下所示:
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);
}
实体类为:
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 + "}";
}
}
调用findOne或findByName时,出现以下错误:
When findOne or findByName is invoked, the following error is given:
org.hibernate.InstantiationException: No default constructor for entity
如何使用Spring JPA存储库而不使用默认构造函数?我想保留实例字段,代码和名称(最终的).
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.
推荐答案
我将创建一个名为MyDto
的单独类,该类具有JSON内容,但没有Entity批注.使其字段也保持最终状态.
I would create a separate class called MyDto
which has the JSON stuff, but not the Entity annotations. Make it's fields final as well.
然后您的存储库方法将如下所示:
Then your repository methods would be something like this:
@Query("SELECT new MyDto(m.code, m.name) FROM My m WHERE m.code = :code")
public MyDto findByCode(@Param("code") String code);
那样,您仅使用My Entity类为您提供到数据库列的映射,而不创建My
的实例.
That way, you are only using the My Entity class to give you the mapping to the database columns, not creating an instance of My
.
Another approach (as detailed here) is to use the Entity class itself as the DTO.
因此您的查询方法可能如下所示:
So your query method could look like this:
@Query("SELECT new My(m.code, m.name) FROM My m WHERE m.code = :code")
public My findByCode(@Param("code") String code);
这具有不必创建单独的DTO类的优点.
This has the advantage of not having to create a separate DTO class.
这篇关于如何在Spring JPA存储库中使用构造函数映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!