0

I am getting a strange error. I created a simple JSF composite component, that is:

     <?xml version="1.0" encoding="ISO-8859-1" ?>  
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">

<cc:interface>
    <cc:attribute name="fieldId" required="true" />
    <cc:attribute name="targetValue" type="java.sql.Date" required="true" />
    <cc:attribute name="required" default="false" />
    <cc:attribute name="disabled" default="false" />
    <cc:attribute name="styleClass" />
</cc:interface>

<cc:implementation>
    <p:calendar id="#{cc.attrs.fieldId}" disabled="#{cc.attrs.disabled}" required="#{cc.attrs.required}" locale="pt" navigator="true" pattern="dd/MM/yyyy" showOn="button" value="#{cc.attrs.targetValue}" styleClass="#{cc.attrs.styleClass}">
        <f:convertDateTime type="date" dateStyle="short" pattern="dd/MM/yyyy"/> 
        <f:ajax event="blur" execute="@this" render="@this" />
    </p:calendar>
</cc:implementation>

    </html>

And I´m using this on my form, normally:

                    <p:outputLabel for="inputInicial:dataInicial" value="#{msg['entity.dataInicial']}" />
                    <po:inputData id="inputInicial" targetValue="#{acaoController.entity.dataInicial}" fieldId="dataInicial"/>

                    <p:outputLabel for="inputFinal:dataFinal" value="#{msg['entity.dataFinal']}" />
                    <po:inputData id="inputFinal" targetValue="#{acaoController.entity.dataFinal}" fieldId="dataFinal" />

And, it works fine when I click to add a new entity... I click on save, and it saves as expected.

When I click to edit an existing entity, and click on save button... This errors occurs:

    SEVERE: Servlet.service() for servlet [facesServlet] in context with path [/ProjetoOlimpio] threw exception [javax.el.ELException: /resources/olimpio/inputData.xhtml @18,238 value="#{cc.attrs.targetValue}": Cannot convert 30/12/12 21:00 of type class java.util.Date to class java.sql.Date] with root cause
    javax.el.ELException: /resources/olimpio/inputData.xhtml @18,238 value="#{cc.attrs.targetValue}": Cannot convert 30/12/12 21:00 of type class java.util.Date to class java.sql.Date
        at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:139)
        at javax.faces.component.UIInput.updateModel(UIInput.java:818)
        at javax.faces.component.UIInput.processUpdates(UIInput.java:735)
        at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
        at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
        at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
        at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
        at javax.faces.component.UIForm.processUpdates(UIForm.java:281)
        at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
        at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
        at org.primefaces.component.layout.Layout.processUpdates(Layout.java:252)
        at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
        at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1242)
        at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1231)
        at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)

My model uses java.util.Date class of course.

    import java.util.Date;
    [...]

@Column(name = "data_inicial")
@Temporal(TemporalType.DATE)
private Date dataInicial;

@Column(name = "data_final")
@Temporal(TemporalType.DATE)
private Date dataFinal;

I am using Spring Data to persist! It is hard to understand...

Why does it work on save (not on update) ? Why does it show the selected Date correctly on the view after I click to edit the entity?

Here some info about my enviroment: INFO: Starting Servlet Engine: Apache Tomcat/7.0.12 INFO: Initializing Mojarra 2.1.10 INFO: Running on PrimeFaces 3.4.2 INFO: Running on PrimeFaces Extensions 0.6.1

**EDIT

1 - As I said on the comments: the problem is with the composite component, if I put the p:calendar directly on the form it works. So I´ll edit the code snippet of the CC to the whole thing! :)

Notice that I have a type="java.sql.Date" That was added because I was testing. Originally it had no type attribute. I also tested with type="java.util.Date.

Marco Noronha
  • 115
  • 1
  • 9
  • 31
  • 1
    I'd say that Spring Data is to blame here that it has populated the property with a `java.sql.Date` instead of `java.util.Date`. See also this Hibernate-related question: http://stackoverflow.com/questions/9533935/how-to-force-hibernate-to-return-dates-as-java-util-date-instead-of-timestamp – BalusC Jan 04 '13 at 18:17
  • 1
    Thank you for the quick reply! I guess your right. But!! Why doesn´t break on page render, not on form submit? ...I am thinking on doing this to solve the problem: return new Date(dataInicial.getTime()); on the Date - type getters. Ugly right? – Marco Noronha Jan 04 '13 at 18:31
  • Because upon creating it is `null` and thus JSF will create new one. – BalusC Jan 04 '13 at 18:33
  • This is very strange. I have one Type on my class, and Hibernate return another!? A converter might solve this problem? – Marco Noronha Jan 04 '13 at 18:40
  • I'm not sure why you're confused. Do you realize that `java.sql.Date` is a [subclass](http://docs.oracle.com/javase/6/docs/api/java/sql/Date.html) of `java.util.Date` and so it's for the JPA implementation "perfectly legal" to fill such a property with `java.sql.Date` instance instead of `java.util.Date` instance? By the way, you aren't using Hibernate, you mentioned yourself that you're using Spring Data. – BalusC Jan 04 '13 at 18:41
  • Thanks for the reply. :) I´ll search for a converter for this. How should we close the topic? (I mean, it is partially solved)... [ Sorry, I´m new to stackoverflow] – Marco Noronha Jan 04 '13 at 18:56
  • If nobody else chimes in in the meanwhile, just post your own answer if you have ultimately found the solution for your particular setup. – BalusC Jan 04 '13 at 18:58
  • Balus, the inverse is happening. The Data type is java.util.Date !! And it can´t convert to java.sql.Date !! So, Spring Data is returning correctly!! – Marco Noronha Jan 05 '13 at 11:50
  • Oh, that's kind of odd. What's the `` of the value? Is it `java.util.Date`? – BalusC Jan 05 '13 at 11:51
  • I didn´t add any type. So I just tested with java.util.Date. Didn´t work. Tested also with java.sql.Date. Same outcome. Any ideas? – Marco Noronha Jan 05 '13 at 11:55
  • How did you determine that it's a `java.util.Date` and not `java.sql.Date`? Did you debug/print `getClass()` or `instanceof`? – BalusC Jan 05 '13 at 11:57
  • By reading the exception. And I added a log.debug to check with instanceof. :( – Marco Noronha Jan 05 '13 at 12:06
  • The `java.sql.Date` is as said a subclass of `java.util.Date`, so it would also always pass the `instanceof` check. Use `getClass()` check instead. – BalusC Jan 05 '13 at 12:09
  • 10:12:41 (AcaoController.java:139) a-DEBUG - Util 10:12:41 (AcaoController.java:140) a-DEBUG - class java.util.Date *both instanceof and getClass* – Marco Noronha Jan 05 '13 at 12:13
  • BalusC !! I figured out the problem is with the composite component!! If I use just the p:calendar directly on the form... It works! Can you help me to fix the composite component? I´ll edit the question and put the whole implementation of the .xhtml class. Thank you in advanced! – Marco Noronha Jan 05 '13 at 12:52
  • You *have* set the `` to `java.sql.Date`! – BalusC Jan 05 '13 at 14:23
  • I tried already :( Same error! – Marco Noronha Jan 05 '13 at 23:04

1 Answers1

2

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>

<cc:implementation>

... <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;
}

}