-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: BigDecimal, FilterDef and Lucene
PostPosted: Wed Feb 02, 2011 4:22 am 
Beginner
Beginner

Joined: Wed Sep 30, 2009 5:29 am
Posts: 29
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


Top
 Profile  
 
 Post subject: Re: BigDecimal, FilterDef and Lucene
PostPosted: Wed Feb 02, 2011 12:50 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi Nick,
indeed a test is missing to cover the BigDecimal case with @NumericField, would you please create a JIRA issue?
Also if you could create a trivial unit test, that would be great.

http://community.jboss.org/wiki/ContributingtoHibernateSearch

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: BigDecimal, FilterDef and Lucene
PostPosted: Wed Feb 02, 2011 1:18 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi Nick,

there are several things at play here. First the numeric field question. As Sanne was saying, there might be an issue with that.
Regarding your filter definition there is another configuration error. First off, you are actually using Hibernate Core filters here which are independent of the Hibernate Search fulltext query filters (which are based on Lucene filters). You probably are aware of this. Just thought I mention it :)
The reason the type registration fails is, because you specify the wrong package. BigDecimal is not in java.lang, but rather in java.math. If you change the package it should work. You could also use the Hibernate internal name of the type - "big_decimal".

--Hardy


Top
 Profile  
 
 Post subject: Re: BigDecimal, FilterDef and Lucene
PostPosted: Thu Feb 03, 2011 12:09 am 
Beginner
Beginner

Joined: Wed Sep 30, 2009 5:29 am
Posts: 29
hardy.ferentschik wrote:
Hi Nick,
there are several things at play here. First the numeric field question. As Sanne was saying, there might be an issue with that.
Regarding your filter definition there is another configuration error. First off, you are actually using Hibernate Core filters here which are independent of the Hibernate Search fulltext query filters (which are based on Lucene filters). You probably are aware of this. Just thought I mention it :)

Good point. I would prefer to do all my searching against Lucene if possible, but I decided as a first step to use Hibernate Search capabilities for the BigDecimal type while experimenting with this (new to me) feature.

hardy.ferentschik wrote:
The reason the type registration fails is, because you specify the wrong package. BigDecimal is not in java.lang, but rather in java.math. If you change the package it should work. You could also use the Hibernate internal name of the type - "big_decimal".

Akk, embarassing. I've changed it to java.math and the filter now seems to work fine against entity attributes of type BigDecimal. Looks like the default schema generated in MySQL is DECIMAL(19,2) which should be fine for my simple financial purposes.

I'll reply to Sanne's message about a unit test also. Thanks Hardy :)


Top
 Profile  
 
 Post subject: Re: BigDecimal, FilterDef and Lucene
PostPosted: Thu Feb 03, 2011 12:58 am 
Beginner
Beginner

Joined: Wed Sep 30, 2009 5:29 am
Posts: 29
s.grinovero wrote:
Hi Nick,
indeed a test is missing to cover the BigDecimal case with @NumericField, would you please create a JIRA issue?

Created http://opensource.atlassian.com/project ... SEARCH-678

s.grinovero wrote:
Also if you could create a trivial unit test, that would be great.

I'll try. I think I fixed my maven slowness problem. It would take 20 minutes to work its way through 4 failed attempts at one pom until one succeeded:

Code:
https://repository.jboss.org/nexus/content/groups/public-jboss//com/uwyn/jhighlight/1.0/jhighlight-1.0.pom
http://download.java.net/maven/glassfish/com/uwyn/jhighlight/1.0/jhighlight-1.0.pom
http://download.java.net/maven/1/com.uwyn/poms/jhighlight-1.0.pom
https://repository.jboss.org/nexus/content/groups/public-jboss//com/uwyn/jhighlight/1.0/jhighlight-1.0.pom
OK: http://repo1.maven.org/maven2/com/uwyn/jhighlight/1.0/jhighlight-1.0.pom


Turns out, my ~/.m2/settings.xml file had a demo "proxy.somewhere.com" proxy set up and enabled, so I presume it was trying this non existent proxy for every attempt, which of course failed. Gah! :)


Top
 Profile  
 
 Post subject: Re: BigDecimal, FilterDef and Lucene
PostPosted: Fri Feb 04, 2011 10:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
neek wrote:
Good point. I would prefer to do all my searching against Lucene if possible, but I decided as a first step to use Hibernate Search capabilities for the BigDecimal type while experimenting with this (new to me) feature.

Good point. We always recommend to index all you need to drive all your searches via Hibernate Search.


Top
 Profile  
 
 Post subject: Re: BigDecimal, FilterDef and Lucene
PostPosted: Fri Feb 04, 2011 12:27 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Relevant issue & discussion :
http://opensource.atlassian.com/projects/hibernate/browse/HSEARCH-678

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.