我使用spring-data-solr将spring-data-jpa与solr集成,但是当我使用SolrOperations来保存bundle(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,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:无效的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:无效的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;
}
}
答案 0 :(得分:0)
问题出在你的solrj Field注释中。看看documentation:
@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;
}