0

我使用spring-data-solr将 spring-data-jpa 与 solr 集成,但是当我使用 SolrOperations 来 saveBean(org.domain.Article) 时,会抛出异常:

org.springframework.data.solr.UncategorizedSolrException:无法从类型 org.kb.domain.Article 转换为类型 org.apache.solr.common.SolrInputDocument 值'Article [id = 1,title = test-1,description = test-1,内容=test-1,作者=test-1,链接=test-1,附件=test-1,日期=2013 年 1 月 5 日星期六 20:06:12 CST,类别=org.kb.domain.Category @67e6cf07]'; 嵌套异常是 org.apache.solr.client.solrj.beans.BindingException: Invalid setter 方法。必须只有一个参数;嵌套异常是 org.springframework.core.convert.ConversionFailedException: 无法从类型 org.kb.domain.Article 转换为类型 org.apache.solr.common.SolrInputDocument 值'Article [id=1,title=test-1 , description=test-1, content=test-1, author=test-1, link=test-1, attachment=test-1, date=Sat Jan 05 20:06:12 CST 2013, category=org.kb。domain.Category@67e6cf07]'; 嵌套异常是 org.apache.solr.client.solrj.beans.BindingException: Invalid setter 方法。必须只有一个参数

这是我的豆:

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.solr.client.solrj.beans.Field;
import com.fasterxml.jackson.annotation.JsonFormat;
@Entity
@Table(name="article")
public class Article extends IdEntity{

private static final long serialVersionUID = -5170398606065544445L;

private String title;

private String description;

private String content;

private String author;

private String link;

private String attachment;

private Date date;

private Category category;

public Article() {
    super();
}

@ManyToOne
@JoinColumn(name="category_id")
public Category getCategory() {
    return category;
}
public void setCategory(Category category) {
    this.category = category;
}

@Column(name="title")
@Field("title")
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@Column(name="description")
@Field("description")
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

@Column(name="content")
@Field("content")
public String getContent() {
    return content;
}
public void setContent(String content) {
    this.content = content;
}

@Column(name="author")
@Field("author")
public String getAuthor() {
    return author;
}
public void setAuthor(String author) {
    this.author = author;
}

@Column(name="link")
@Field("link")
public String getLink() {
    return link;
}
public void setLink(String link) {
    this.link = link;
}

@Column(name="attachment")
@Field("attachment")
public String getAttachment() {
    return attachment;
}

public void setAttachment(String attachment) {
    this.attachment = attachment;
}

@Column(name="date")
@JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+08:00")
public Date getDate() {
    return date;
}
public void setDate(Date date) {
    this.date = date;
}

@Override
public String toString() {
    return "Article [id=" + id + ",title=" + title + ", description=" +     description
            + ", content=" + content + ", author=" + author + ", link="
            + link + ", attachment=" + attachment + ", date=" + date
            + ", category=" + category + "]";
}

}

import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.apache.solr.client.solrj.beans.Field;

@MappedSuperclass
public abstract class IdEntity implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = -5676694680777491651L;
protected Long id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Field("id")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}
}
4

1 回答 1

0

问题出在您的 solrj Field 注释中。看看文档

@Field 注解可以应用于字段或 setter 方法。

您应该将 Field 注释移动到 setId setter 方法或 id 字段本身。您甚至可以删除 id 限定符,因为字段名称已经是 id,这就足够了:

@Field
protected Long id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}
于 2013-01-07T10:17:38.803 回答