Cannot convert 4/23/12 12:00 AM of type class java.util.Date to class java.sql.Date
Asked Answered
O

2

5

I'm migrating my project from WebSphere 7 to WebSphere 8 and I'm using JSF 1.2.

I was facing a problem with IBM JSF/html_extended tags and also standard converters, which are mainly JSF 1.2 core components. I'm also updating my Java EE version from 5 to 6 (which might not be the reason). Finally, there is also a component tree given.

Below is my stack trace:

javax.faces.component.UpdateModelException: org.apache.jasper.el.JspELException: /sc40/NewContract.jsp(130,5) '#{pc_NewContract.overrideAsOfDtSQL}' Cannot convert 4/23/12 12:00 AM of type class java.util.Date to class java.sql.Date
    at javax.faces.component.UIInput.updateModel(UIInput.java:398)
    at javax.faces.component.UIInput.processUpdates(UIInput.java:299)
    at javax.faces.component.UIForm.processUpdates(UIForm.java:187)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1258)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1258)
    at javax.faces.component.UIViewRoot._processUpdatesDefault(UIViewRoot.java:1321)
    at javax.faces.component.UIViewRoot.access$600(UIViewRoot.java:75)
    at javax.faces.component.UIViewRoot$UpdateModelPhaseProcessor.process(UIViewRoot.java:1423)
    at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1282)
    at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:765)
    at org.apache.myfaces.lifecycle.UpdateModelValuesExecutor.execute(UpdateModelValuesExecutor.java:34)
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1147)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:722)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:449)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1020)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3639)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:950)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1659)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:816)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1648)
Caused by: org.apache.jasper.el.JspELException: /sc40/NewContract.jsp(130,5) '#{pc_NewContract.overrideAsOfDtSQL}' Cannot convert 4/23/12 12:00 AM of type class java.util.Date to class java.sql.Date
    at org.apache.jasper.el.JspValueExpression.setValue(JspValueExpression.java:98)
    at javax.faces.component.UIInput.updateModel(UIInput.java:380)
    ... 35 more

Overwrought answered 23/4, 2012 at 19:10 Comment(0)
L
8

'#{pc_NewContract.overrideAsOfDtSQL}' Cannot convert 4/23/12 12:00 AM of type class java.util.Date to class java.sql.Date

You apparently have a

private java.sql.Date overrideAsOfDtSQL;

This is not correct. The java.sql.* types do not belong in the model. Replace it by java.util.Date.

private java.util.Date overrideAsOfDtSQL;

The same answer applies when you're using java.sql.Time.

Note that java.sql.Date and java.sql.Time are subclasses of java.util.Date, that's why it worked when converting from object to string with <f:convertDateTime>. Only converting from string to object won't work because the <f:convertDateTime> always converts to java.util.Date.

Lanctot answered 2/5, 2012 at 16:56 Comment(1)
I had used java.lang.Calendar for timestamps which I had then annotated with @Temporal (TemporalType.TIMESTAMP). Changing to java.util.Date was here required to make p:calendar happy again (else a similar exception will come.Antimalarial
F
0

There is workaround without changing hibernate model. I prefer this way because all changes are in jsf layer.

You can use binding in composite component. Next code is example with rich:calendar (which uses java.util.Date)

... <cc:interface componentType="CalendarComponent">

... </cc:interface>

&lt;cc:implementation&gt;

... <rich:calendar value="#{cc.attrs.value}" binding="#{cc.attrs.calendar}" />

...
</cc:implementation>

...

and in CalendarComponent:

import java.util.Date;

import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;

import org.richfaces.component.UICalendar;

@FacesComponent(value = "CalendarComponent")
public class CalendarComponent extends UINamingContainer {

@Override
public void processUpdates(FacesContext context) {

Object o = calendar.getValue();
   if (o instanceof Date) {
  Date d = (Date) o;
                    //this ensures type changing  
        calendar.setValue(new java.sql.Date(d.getTime()));
}
    super.processUpdates(context);
}

private UICalendar calendar;

public UICalendar getCalendar() {
    return calendar;
}

public void setCalendar(UICalendar calendar) {
    this.calendar = calendar;
}

}
Feder answered 1/11, 2013 at 8:44 Comment(1)
This effectively only strips the time part from the date. I'm not seeing how that's ever useful if you can just use <f:convertDateTime> for it.Lanctot

© 2022 - 2024 — McMap. All rights reserved.