[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-6873) java.util.Date is fetched as java.sql.Timestamp from database which causes custom equal() method to fail

Martin Burger (JIRA) noreply at atlassian.com
Fri Dec 2 06:29:19 EST 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-6873?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=44495#comment-44495 ] 

Martin Burger commented on HHH-6873:
------------------------------------

I might have stumbled upon http://stackoverflow.com/a/231434/66981.

However, a quick workaround for both cases would be:

  org.apache.commons.lang3.time.DateUtils.truncatedEquals(a, b, Calendar.SECOND);



> java.util.Date is fetched as java.sql.Timestamp from database which causes custom equal() method to fail
> --------------------------------------------------------------------------------------------------------
>
>                 Key: HHH-6873
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6873
>             Project: Hibernate Core
>          Issue Type: Bug
>    Affects Versions: 3.6.8
>            Reporter: Martin Burger
>         Attachments: SimpleState.java
>
>
> I have class SimpleState (in my actual application, that state class is more complex) that represents a state. A state consists of an Integer value (SimpleState.state) and a java.util.Date instance (SimpleState.created); the latter tells when the state was created.
> Unfortunately, after Hibernate has fetched a persistent instance of SimpleState from the database, SimpleState.created refers to an instance of java.sql.Timestamp instead of java.util.Date as expected. This causes SimpleState's equals() method to fail in the following situation:
> 1. Persist SimpleState state with Hibernate in session #1.
> 2. Fetch state as state* from db via session #2 (thus, new session which is different from session #1).
> 3. Check for equality by calling state.equals(state*).
> I would expect that state.equals(state*) holds, but it does not (output as printed by SimpleState.equals()):
> class java.util.Date != class java.sql.Timestamp
> Fri Dec 02 11:11:43 CET 2011 != 2011-12-02 11:11:43.0
> 1322820703319 != 1322820703000
> Some people recommend to use Joda Time or to use a custom type / converter as workaround. However, I would prefer to keep my model totally independent from any constrain that would be implied by some persistence framework. Otherwise, I would have to keep all possible persistence frameworks (and all their respective peculiarities) in mind whenever I would create a model.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list