2013-01-05 47 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; 
} 
相關問題