[hibernate-issues] [Hibernate-JIRA] Commented: (HSEARCH-678) @NumericField does not work with BigDecimal types (and others?)

G Fernandes (JIRA) noreply at atlassian.com
Sat Feb 5 12:38:05 EST 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HSEARCH-678?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=39831#action_39831 ] 

G Fernandes commented on HSEARCH-678:
-------------------------------------

Here's a way to achieve that. Suppose an entity with a BigDecimal, you could use a bridge to do the conversion to long:

{noformat}
@Entity @Indexed
public class Price {

        @Id
	private int id;

	@Field @NumericField
	@FieldBridge(impl = BigDecimalNumericFieldBridge.class)
	private BigDecimal val;

	public Price() {}

	public Price(int id, String price) {
		this.id = id;
		this.val = new BigDecimal(price);
	}
}
{noformat}

The Bridge would simply multiply per 100 when indexing:

{noformat}
public class BigDecimalNumericFieldBridge extends NumericFieldBridge {

	@Override
	public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
		if (value != null) {
			BigDecimal decimalValue = (BigDecimal) value;
			long indexedValue = decimalValue.multiply(BigDecimal.valueOf(100)).longValue();
			luceneOptions.addNumericFieldToDocument(name, indexedValue, document);
		}
	}
}
{noformat}

The only caveat is that you need to remember multiply by 100 when you do the queries.



> @NumericField does not work with BigDecimal types (and others?)
> ---------------------------------------------------------------
>
>                 Key: HSEARCH-678
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HSEARCH-678
>             Project: Hibernate Search
>          Issue Type: Bug
>          Components: mapping, query
>    Affects Versions: 3.3.0.Final
>         Environment: Hibernate 3.6.0, MySQL 5.1.52, Fedora Core 14
>            Reporter: Nick Fenwick
>            Assignee: G Fernandes
>             Fix For: 3.4.0
>
>         Attachments: HSEARCH-678-unittest1.tgz
>
>
> This is my first JIRA so please help me assigns its Component above correctly.
> My first attempt to use BigDecimal ran into trouble trying to use the new @NumericField annotation.  The SessionFactory fails to create an instance of the session.  The code:
> {{
> @Column(name="val")
> @Field(name="val", index=Index.UN_TOKENIZED, store=Store.YES)
> @NumericField
> private BigDecimal val;
> }}
> Results in the exception:
> {{
> 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]
> }}
> Taking out the @NumericField allows the session to start up successfully.
> I will attempt to make a test case but I'm having local build env issues.  'mvn clean install' of a fresh hibernate search git checkout is taking an incredibly long time (an hour for the first 10 or so .pom files?) so I may be several hours or days over this trivial task.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list