Solr - 无法设置java.sql.Timestamp字段...创建到java.util.Date

时间:2012-10-15 14:59:51

标签: solr solrj

我无法从QueryResponse中读取时间戳数据(想要将结果转换为POJO)。如果我使用SolrDocumentList则没有错误。

分贝数据-config.xml中:

<entity name="discussion" dataSource="mssqlDatasource" pk="id"
        transformer="TemplateTransformer, DateFormatTransformer"
        query="SELECT d.id, d.title, d.created,
               u.first_name, u.last_name,
               db.type AS boardType, db.country
               FROM discussion d
               INNER JOIN wtb_user u ON d.author = u.id
               INNER JOIN discussion_category dc ON d.category = dc.id
               INNER JOIN discussion_board db ON dc.board = db.id">                   
        <!--<field column="created" dateTimeFormat='yyyyMMdd HH:mm:ss z' />-->
        <!--<field column="created" dateTimeFormat='yyyyMMdd' />-->
        <field column="first_name" name="firstName" />
        <field column="last_name" name="lastName" />
        <field column="tableType" template="DISCUSSION" />

        <entity name="discussion_post" dataSource="mssqlDatasource"
            transformer="ClobTransformer"
            query="SELECT dp.message
                   FROM discussion_post dp
                   WHERE dp.discussion = '${discussion.id}'">
            <field column="message" name="discussionPostMessage" clob="true" />
        </entity>
    </entity>

schema.xml中:

<field name="created" type="date" indexed="false" stored="true"/> 

字段'created'是我的数据库中的时间戳,插入索引数据后结果如下(使用浏览器管理控制台调用):

  <result name="response" numFound="246" start="0">
  <doc>
   <str name="boardType">1</str>
   <date name="created">2012-10-05T07:29:23.387Z</date>
   <arr name="discussionPostMessage">
    <str>message test</str>
    <str>second</str>
    <str>third</str>
   </arr>
   <str name="firstName">Ashley</str>
   <str name="id">10</str>
   <str name="lastName">Morgan</str>
   <str name="tableType">DISCUSSION</str>
   <str name="title">headline test</str>
  </doc>
  ...

现在我试图查询'所有结果'

public void search(String searchString) {
    SolrQuery query = new SolrQuery();
    QueryResponse rsp;
    try {
        query = new SolrQuery();
        query.setQuery(DEFAULT_QUERY);
        query.setRows(246);

        rsp = getServer().query(query);

        SolrDocumentList solrDocumentList = rsp.getResults(); // IllegalArgumentException
        List<SearchRequestResponseObject> beans = rsp.getBeans(SearchRequestResponseObject.class); //works
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

SearchRequestResponseObject.class:

public class SearchRequestResponseObject {
@Field
private String id;

@Field
private String title;

@Field
@Temporal(TemporalType.TIMESTAMP)
    //@DateTimeFormat(style = "yyyyMMdd HH:mm:ss z")
//@DateTimeFormat(style = "yyyyMMdd")
private Timestamp created;
    ...
}

例外:

Caused by: java.lang.IllegalArgumentException: Can not set java.sql.Timestamp field com.solr.SearchRequestResponseObject.created to java.util.Date
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
at java.lang.reflect.Field.set(Field.java:657)
at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:374)
... 45 more

详细信息:

Exception while setting value : Fri Oct 05 09:29:23 CEST 2012 on private java.sql.Timestamp com.solr.SearchRequestResponseObject.created

我错了什么?为什么要“创建”一个日期?

1 个答案:

答案 0 :(得分:2)

您的“已创建”字段应该是一个日期,因为即使您从数据库传入TimeStamp,它也会在内部存储为Solr中的Date,因此将作为Solr的日期公开。 SolrJ不了解如何处理TimeStamp。如果你真的想在你的POJO上拥有TimeStamp属性,你需要将Solr中的字段作为日期,然后在你的POJO上有一个单独的TimeStamp属性来转换Date值。