0

I am experiencing problems with java.lang.ClassCastException: java.sql.Date cannot be cast to java.time.LocalDate exceptions when I execute the following code in my JavaFX application form (where dpStartDate is my DatePicker): this.dpStartDate.valueProperty().bindBidirectional(this.productPriceGroup.startDateProperty());

In my ProductPriceGroup Hibernate Entity class I have the following code:

public class ProductPriceGroup {
...
    private SimpleObjectProperty<LocalDate> startDate = new SimpleObjectProperty();
...
    //@Temporal(TemporalType.DATE)
    @Column(name = "StartDate", nullable = false, length = 10)
    public LocalDate getStartDate() {
        return this.startDate.get();
    }

    public void setStartDate(LocalDate startDate) {
        this.startDate.set(startDate);
    }
...
    public SimpleObjectProperty<LocalDate> startDateProperty() {
        return this.startDate;
    }
}

In my ProductPriceGroup class I have tried making everything a LocalDate rather than a Date (so there is no casting) but then I get the following exception when I launch my application: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: com.flextor.backoffice.model.products.ProductPriceGroup.startDate

Can somebody please help me solve this tedious problem.

UPDATE: Thank you for your help Damian. I have removed the @Temporal annotation as per the attached answer. I have changed everything in my Entity from using a java.util.Date to using a LocalDate. The problem I am now having is when I try to run the following code:

public ProductPriceGroup_vwList(Session session, int brandKey, int countryKey, int productLineKey) {
    String sql = "FROM ProductPriceGroup_vw vw WHERE vw.brandKey =:brandKey AND vw.countryKey =:countryKey AND vw.productLineKey = :productLineKey";
    Query query = session.createQuery(sql);
    query.setParameter("brandKey", brandKey);
    query.setParameter("countryKey", countryKey);
    query.setParameter("productLineKey", productLineKey);
    this.setRecordList(query.list());
}

The last line produces the following Exception:

org.hibernate.type.SerializationException: could not deserialize
    at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:262)
    at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:306)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:155)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:130)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:44)
    at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:71)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:263)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:259)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:249)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:334)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1695)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1627)
    at org.hibernate.loader.Loader.getRow(Loader.java:1514)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:952)
    at org.hibernate.loader.Loader.doQuery(Loader.java:920)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2551)
    at org.hibernate.loader.Loader.doList(Loader.java:2537)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367)
    at org.hibernate.loader.Loader.list(Loader.java:2362)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at com.flextor.backoffice.model.products.ProductPriceGroup_vwList.<init>(ProductPriceGroup_vwList.java:37)
    at com.flextor.backoffice.organisations.ProductPriceGroupSubform.init(ProductPriceGroupSubform.java:32)
    at com.flextor.backoffice.organisations.BrandForm.doPostLoad(BrandForm.java:235)
    at com.commons.forms.AbstractForm.doLoad(AbstractForm.java:57)
    at com.commons.forms.AbstractForm.init(AbstractForm.java:66)
    at com.flextor.backoffice.organisations.BrandSelectionForm.doEdit(BrandSelectionForm.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.commons.forms.AbstractRecordSelectionForm.onRowDoubleClicked(AbstractRecordSelectionForm.java:589)
    at com.commons.forms.AbstractRecordSelectionForm.lambda$doPreLoad$2(AbstractRecordSelectionForm.java:269)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.StreamCorruptedException: invalid stream header: 32303138
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:863)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:355)
    at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:328)
    at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:318)
    at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:237)
    ... 93 more

I think I understand the problem: I need to convert from a java.sql.Date (which is the data type of the column in the database) to a LocalDate. My challenge is that I don't know where to do it, while at the same time being able to bind the startDateProperty to my DatePicker control. I have tried in various places using .toLocalDate() but I just end up tying myself up in knots. I am sure there must be a simple way to do this, I just can't work it out. Any help would be much appreciated. I have changed the previously supplied code to reflect my current situation.

Regards, Stuart

StuartM
  • 57
  • 1
  • 7
  • Possible duplicate of [Hibernate @Temporal for Java 8 java.time.Instant](https://stackoverflow.com/questions/35507985/hibernate-temporal-for-java-8-java-time-instant) – fabian Sep 03 '18 at 20:54
  • Also https://stackoverflow.com/questions/907360/explanation-of-classcastexception-in-java and https://stackoverflow.com/questions/29750861/convert-between-localdate-and-sql-date/29751575 – fabian Sep 03 '18 at 20:55
  • Thanks for the help Damian. I have implemented your suggestions but I am now having an exception. Please see my updates to the question. Any further help appreciated. – StuartM Sep 08 '18 at 02:27

0 Answers0