Reputation: 567
I cannot read timestamp data from QueryResponse (want to cast result to a POJO). If Im using SolrDocumentList there are no errors.
db-data-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"/>
the field 'created' is a timestamp in my database and after inserting index data a result looks like (called with browser admin console) :
<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>
...
Now I tried to to query a 'all result'
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;
...
}
Exception:
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
Detail Message:
Exception while setting value : Fri Oct 05 09:29:23 CEST 2012 on private java.sql.Timestamp com.solr.SearchRequestResponseObject.created
What do Im wrong? Why should be 'created' a Date?
Upvotes: 0
Views: 2647
Reputation: 22555
Your 'created' field should be a date because even though you are passing in a TimeStamp from your database, it is being stored internally as a Date in Solr and is therefore being exposed as a Date from Solr. SolrJ does not understand how to handle a TimeStamp. If you really want to have TimeStamp property on your POJO, you will need to get the field from Solr as a Date and then have a separate TimeStamp property on your POJO that converts the Date value.
Upvotes: 2