3

I have a SQLServer and am using hibernate to map an entity to a table that I already have. the table has a field of type "timestamp" [field_date] timestamp NOT NULL which is the only date the table has.

I have tried mapping it on an entity like this:

@Column(name="field_date")
private Date date;

and even

@Column(name="field_date",columnDefinition="TIMESTAMP")
private Date date;

but every time I try to do a select on that entity I get an SQLServerException of type

 Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from timestamp to TIMESTAMP is unsupported.
2
  • 2
    Whenever I use dates, I always use the @Temporal annotation to define what type it is. That hasn't failed me yet, but I have never used SQLServer so I don't feel comfortable providing it as an answer. Commented Dec 18, 2013 at 9:37
  • @Gimby yes, indeed @ Temporal, as peter suggested as well, proved to be the best solution in this case as well.Thank you nonetheless Commented Dec 18, 2013 at 11:03

1 Answer 1

7

1) Try mapping it to the Java class java.sql.Timestamp.

http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html

Also here I would try removing this from your annotation
columnDefinition="TIMESTAMP"

2) Here is another mapping to try.
So alternatively you can try this approach.

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CreatedOn", nullable = false)
public Date getCreatedOn() {
    return this.createdOn;
}

public void setCreatedOn(Date createdOn) {
    this.createdOn = createdOn;
}

3) This is like 2) but it is to be used if you want
mapping for the class field (not for the getter).

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "Last_Updated_Date", nullable=false)
private Date lastUpdatedDate;
Sign up to request clarification or add additional context in comments.

1 Comment

Hello peter and thank you, your first suggestion did not work and gave the same exception, the @temporal annotation tough worked.Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.