使用spring实体

时间:2016-11-05 06:41:01

标签: java spring

所以我正在为一个名为CodeCenterApplications的实体进行投影。见下面的实体

@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()应该引用该实体,而是认为它正在尝试获取String的属性。

另外,这是我的第一个问题,欢迎批评。

1 个答案:

答案 0 :(得分:0)

Hibernate是一个将Java对象(实体对象)映射到关系数据库的ORM框架,您没有将CodeCenterApplication的所有属性(名称,版本等)映射到数据库表。 / p>

在使用注释时,需要使用@Column将属性映射到数据库表列,如下所示:

@Column(name="YOUR_TABLE_COLUMN")

protected String name;

//other properties also need to be mapped including insertionDate

您可以查看here