[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-4179) JPQL Constructor Queries containing Timestamp cause exception

Maillefer Jean-David (JIRA) noreply at atlassian.com
Fri Nov 13 07:33:08 EST 2009


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

Maillefer Jean-David commented on HHH-4179:
-------------------------------------------

Effectively, the workaround (adding a constructor with java.util.Date instead of java.sql.Timestamp) is not portable and (most important) not at all intuitive.

If you have a (SQL) Timestamp in a query, you expect it to be assignable to a java.sql.Timestamp.

And a Timestamp can have precision up to the nanosecond, which is not the case for Date. Thus it is not possible to retrieve the nanoseconds from a Date (only milliseconds are stored).
Thus the workaround is NOT trivial (in response to comments in HHH-278).

> JPQL Constructor Queries containing Timestamp cause exception
> -------------------------------------------------------------
>
>                 Key: HHH-4179
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4179
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: entity-manager
>         Environment: Hibernate 3.3.1.GA, Derby, JDK1.5, Windows.
>            Reporter: Bjorn Beskow
>            Priority: Minor
>         Attachments: hibernate-jpql-timestamp-bug.zip
>
>
> When a constructor JPQL query projects a Timestamp, it causes an org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor.
> For example, given the following Entity:
> @Entity
> public class Employee {
>     ...
>     private Timestamp lastUpdatedAsTimestamp;
>     ...
> }
> and the following Pojo for the constructor query:
>     public EmployeeDto(String name, Timestamp lastUpdated) {...}
> the following query will throw an exception:
>     	String query =
>     		"SELECT new test.dto.EmployeeDtoUsingTimestamp(e.name, e.lastUpdatedAsTimestamp) " +
>           	"FROM Employee e ORDER BY e.name";
>     	List<EmployeeDto> reportList = em.createQuery(query).getResultList();
> java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [test.dto.EmployeeDto] [SELECT new test.dto.EmployeeDto(e.name, e.lastUpdated) FROM test.entities.Employee e ORDER BY e.name]
> 	at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:601)
> 	at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:96)
> 	at test.ConstructorQueryTest.testEmployeesQuery(ConstructorQueryTest.java:63)
>     ...
> This problem has been reported before (see HHH-278), but the workaround is not a portable JPA solution (having the type Timestamp in the Entity and the type Date in the Pojo constructor is not in line with the Spec, and does not work in other JPA implementations).
> I have attached a minimal Maven project with a test case which highlights the problem.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list