Hello,
I have diffucilties searching for a java.util.Date
using Hibernate Search 5.3.0 and Hibernate 4.3.6
Code:
@Entity
public abstract class Gesamtentscheidung extends WIBASObjekt
@DateBridge(resolution = Resolution.DAY)
@Temporal(TemporalType.DATE)
protected Date datum;
When search for this field I get
Code:
org.hibernate.search.bridge.BridgeException: Exception while calling bridge#objectToString
class: de.bwl.uis.module.wasserrecht.entity.Grundentscheidung
path: datum
......
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
at org.hibernate.search.bridge.builtin.NumericEncodingDateBridge.objectToString(NumericEncodingDateBridge.java:74)
at org.hibernate.search.bridge.util.impl.ContextualExceptionBridgeHelper$TwoWayConversionContextImpl.objectToString(ContextualExceptionBridgeHelper.java:133)
... 34 more
??? Why is there a cast from String to Date?
I have tried to implement my own bridge
Code:
public class DateBridge implements StringBridge {
@Override
public String objectToString(Object obj) {
if (!(obj instanceof Date)) {
throw new IllegalArgumentException(obj.getClass().getName() + " is not java.util.Date");
}
Date date = (Date) obj;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String day = "" + calendar.get(Calendar.DAY_OF_MONTH);
if (day.length() == 1) {
day = "0" + day;
}
int m = calendar.get(Calendar.MONTH) + 1;
String month = "" + m;
if (month.length() == 1) {
month = "0" + month;
}
String year = "" + calendar.get(Calendar.YEAR);
String dateString = day + "." + month + "." + year;
return dateString;
}
}
I have debugged the search. The objectToString() method is called two times.
The first time with a Date, as expected, a second time with a String representing the Date.
What is my mistake?