简体   繁体   中英

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM