Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlserver hibernate date mapping

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.
like image 234
sokie Avatar asked May 24 '26 17:05

sokie


1 Answers

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;
like image 164
peter.petrov Avatar answered May 26 '26 05:05

peter.petrov