-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping a date field in an JPA entity @DateBridge
PostPosted: Mon Sep 14, 2015 3:41 am 
Beginner
Beginner

Joined: Sat Feb 16, 2008 3:09 pm
Posts: 24
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?


Top
 Profile  
 
 Post subject: Re: Mapping a date field in an JPA entity @DateBridge
PostPosted: Tue Sep 15, 2015 11:16 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

Your usage looks good to me. Can you share the complete stack trace, as ContextualExceptionBridgeHelper is part of the snippet you showed I believe that another exception is the actual cause.

Thanks,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.