开发者

How to read SQL Server 2008 date into Java using JPA

I need to use java.util.Calendar in order for Axis2 webservice client to send/receive Date objects. So in my JPA model object I changed all java.sql.Date into java.util.Calendar. By using java.util.Calendar the Axis2 webservice part work ok.

I'm able to save a java.util.Calendar object into database. Problem is that I can not read the object back from database!!!

When I try to read a saved object from SQL Server 2008 I get this error:

An error occurred whil开发者_JAVA百科e converting the nvarchar value to JDBC data type TIMESTAMP.

In database the date field where it fails to read is of type "date". Also if in database I change the date field to type "datetime" it works ok, but I to use the "date" type. Anyone has any clues on how to fix this?


So in my JPA model object I changed all java.sql.Date into java.util.Calendar.

Using a java.sql.Date in your domain objects is not really a good idea anyway.

In database the date field where it fails to read is of type "date". Also if in database I change the date field to type "datetime" it works ok, but I to use the "date" type. Anyone has any clues on how to fix this?

Maybe your Calendar is not annotated properly, change it like this:

@Temporal(TemporalType.DATE) 
private java.util.Calendar someDate;

Below, the relevant section from the JPA 1.0 specification:

9.1.20 Temporal Annotation

The Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.

The Temporal annotation may be used in conjunction with the Basic annotation.

The TemporalType enum defines the mapping for these temporal types.

public enum TemporalType {
    DATE, //java.sql.Date
    TIME, //java.sql.Time
    TIMESTAMP //java.sql.Timestamp
}

...

References

  • JPA 1.0 Specification
    • Section 9.1.20 "Temporal Annotation"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜