使用spring实体的java投影错误
所以我对一个名为CodeCenter应用程序的实体进行了一个投影。请参见下面的实体
@Entity
@Table(name = "CODE_CENTER_APPLICATIONS")
public class CodeCenterApplication {
@Id
@Column(columnDefinition = "BIGINT")
protected Integer id;
protected String name;
protected String version;
@JsonFormat(pattern = "yyyy-MM-dd")
protected Date insertionDate;
@ManyToMany
@JoinTable(name = "APPROVALS", joinColumns = {@JoinColumn(name = "APPLICATION_ID", columnDefinition = "BIGINT")},
inverseJoinColumns = {@JoinColumn(name = "API_ID")})
private List<API> apis;
public CodeCenterApplication() {
}
public CodeCenterApplication(Integer id, String name, String version) {
this.id = id;
this.name = name;
this.version = version;
}
public CodeCenterApplication(Integer id, String name, String version, Date insertionDate) {
this.id = id;
this.name = name;
this.version = version;
this.insertionDate = insertionDate;
}
public List<API> getApis() {
return apis;
}
public void setApis(List<API> apis) {
this.apis = apis;
}
/**
* Can be used if you wish to generate a table with the appropriate headers.
*
* @return
*/
@JsonIgnore
public String[] getColumnNames() {
String[] header = {"NAME", "VERSION", "INSERTION_DATE"};
return header;
}
/**
* A giant 'getter' for all all attributes save for id.
*
* @return
*/
@JsonIgnore
public String[] getAttributeList() {
String insertionDateString = "2016-01-01"; //insertionDate.toString();
String[] attributeList = {name, version, insertionDateString};
return attributeList;
}
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String Name) {
this.name = Name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Date getInsertionDate() {
return insertionDate;
}
public void setInsertionDate(Date insertionDate) {
this.insertionDate = insertionDate;
}
}
投影如下,正如你所见,我只想要名字
public interface NameOnly {
String getName();
}
最后,这里是存储库中使用它的方法
/**
* The following strings are used in creating queries.
*/
String fuzzySchema = "\"FROODES2\"";
String fuzzyTable = "\"APPLICATIONS\"";
String fuzzyColumns = "\"NAME\"";
String fuzzySearchInput = ":name";
String fuzzyGroupSearchString = "SELECT " + fuzzyColumns + " FROM " + fuzzySchema + "." + fuzzyTable
+ " WHERE CONTAINS((" + fuzzyColumns + "), " + fuzzySearchInput + ", FUZZY(0.75)) GROUP BY " +
fuzzyColumns + ", SCORE()" + " ORDER BY " + "SCORE() " + "DESC " + "LIMIT :limit OFFSET :offset";
@Query(value = fuzzyGroupSearchString, nativeQuery = true)
List<NameOnly> findByNameGrouped(@Param("name") String name, @Param("offset") int offset, @Param("limit") int
limit);
然而,当我尝试调用这个存储库方法时,我得到了以下错误
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Invalid property 'name' of bean class [java.lang.String]: Could not find field for property during fallback access! (through reference chain: java.util.ArrayList[0]->$Proxy136["name"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid property 'name' of bean class [java.lang.String]: Could not find field for property during fallback access! (through reference chain: java.util.ArrayList[0]->$Proxy136["name"])
我注意到它没有检测到getName()应该是指实体,而是认为它试图获取字符串的属性
此外,这是我的第一个问题,所以欢迎评论
# 1 楼答案
Hibernate是一个ORM框架,它将一个Java对象(实体对象)映射到一个关系数据库,但您没有映射所有属性(名称、版本等)数据库表中的
CodeCenterApplication
在使用注释时,需要使用
@Column
将属性映射到数据库表列,如下所示:你可以看看here