Env: hibernate 3.6.0, search 3.3.0
I'm trying to use BigDecimal for the first time. The Hibernate docs seem to indicate it 'just works'.. there is a default bridge defined for it, so I should be able to use entity attributes of type 'BigDecimal' where attributes of type, say, 'double' worked before.
I used to have something like:
Code:
@Entity
@Table(name="searchable")
@Indexed
@FilterDefs( {
@FilterDef(name = "valBetweenFilter",
defaultCondition = "val BETWEEN :valFrom and :valTo",
parameters = { @ParamDef(name="valFrom", type="java.lang.Double"),
@ParamDef(name="valTo", type="java.lang.Double") } )
} )
@Filters( {
@Filter(name = "valBetweenFilter")
} )
public class SearchableDouble extends AbstractPersistentObject implements Serializable {
@Column(name="val")
@Field(name="val", index=Index.UN_TOKENIZED, store=Store.YES)
@NumericField(precisionStep=4)
private double val;
[etc...]
This worked. I could query Hibernate using the Filter 'valBetweenFilter' as you would expect.
Now, trying to turn the 'double val' into 'BigDecimal val' is giving me errors trying to set the filters parameters. The code:
Code:
@Entity
@Table(name="searchable")
@Indexed
@FilterDefs( {
@FilterDef(name = "valBetweenFilter",
defaultCondition = "val BETWEEN :valFrom and :valTo",
parameters = { @ParamDef(name="valFrom", type="java.lang.BigDecimal"),
@ParamDef(name="valTo", type="java.lang.BigDecimal") } )
} )
@Filters( {
@Filter(name = "valFromFilter", condition = "val > :valFrom"),
@Filter(name = "valToFilter"),
@Filter(name = "valBetweenFilter")
} )
public class SearchableBigDecimal extends AbstractPersistentObject implements Serializable {
@Column(name="val")
@Field(name="val", index=Index.UN_TOKENIZED, store=Store.YES)
private BigDecimal val;
Note I took out @NumericField, see below. Trying to set up my filter, exactly as I did before but with the type changed:
Code:
Filter f = fullTextSession.enableFilter("valBetweenFilter");
f.setParameter("valFrom", new BigDecimal(1.0));
f.setParameter("valTo", new BigDecimal(2.0));
f.validate();
I get the error:
Quote:
SEVERE: Fatal: Undefined filter parameter [valFrom]
java.lang.IllegalArgumentException: Undefined filter parameter [valFrom]
at org.hibernate.impl.FilterImpl.setParameter(FilterImpl.java:98)
at test.searchbigdecimalrange.Main.doSearchUsingFilterDef(Main.java:200)
Putting in some debug code to extract the filter's parameters and their types reveals the type is considered null:
Quote:
FilterDefinition filterDef = f.getFilterDefinition();
Iterator it = filterDef.getParameterNames().iterator();
while (it.hasNext()) {
String name = (String)it.next();
logger.info("debug: param name (" + name + ") - type " + filterDef.getParameterType(name));
}
yields -->
Feb 2, 2011 3:04:20 PM test.searchbigdecimalrange.Main doSearchUsingFilterDef
INFO: debug: param name (valFrom) - type null
Feb 2, 2011 3:04:20 PM test.searchbigdecimalrange.Main doSearchUsingFilterDef
INFO: debug: param name (valTo) - type null
Is it not safe to use BigDecimal in place of normal numeric types?
How does BigDecimal play with the @org.hibernate.search.annotations.NumericField annotation? I tried including it in the 'val' field definition from above, i.e.:
Code:
@Column(name="val")
@Field(name="val", index=Index.UN_TOKENIZED, store=Store.YES)
@NumericField
private BigDecimal val;
With that single line addition, the SessionFactory fails to create, with the error:
Quote:
Exception in thread "main" java.lang.ExceptionInInitializerError
[cut]
Caused by: org.hibernate.HibernateException: could not init listeners
[cut]
Caused by: org.hibernate.search.SearchException: Unable to guess FieldBridge for val
at org.hibernate.search.bridge.BridgeFactory.guessType(BridgeFactory.java:250)
at org.hibernate.search.engine.AbstractDocumentBuilder.bindFieldAnnotation(AbstractDocumentBuilder.java:690)
at org.hibernate.search.engine.AbstractDocumentBuilder.checkForField(AbstractDocumentBuilder.java:564)
[cut]
I've searched the forums and googled for advice, but I'm finding solid examples of how to use BigDecimal with annotated hibernate code hard to find. Plus, the @NumericField stuff is very new, so I thought I'd best ask here.
Nick