2013-01-05 51 views
0

我用spring-data-solr到弹簧数据的JPA与Solr的集成,但是当我使用SolrOperations到saveBean(ORG。 domain.Article),引发异常:org.springframework.data.solr.UncategorizedSolrException:无法转换为类型org.apache.solr.common.SolrInputDocument

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,[email protected]]';嵌套异常是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。 [email protected]]';嵌套异常是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

的问题是在你的solrj场注解。看看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; 
} 
相关问题