hibernate sqlserver timestamp,使用datetime代替datetime2 sqlserver休眠

I have hibernate configured as auto-update. I have a mssql-server 2012. Hibernate creates my java.util.Date objects as datetime2 in the database. Is there a possiblity to force hibernate to create datetime in the database?

I know that I can use annotations on the fields in my object, but there are too many. So I would like to configure hibernate, so that every java.util.Date becomes automatically datetime in the databse.

解决方案

The first thing coming to my mind is defining a new Hibernate Dialect, which maps the Java data types to datetime, instead of the default datetime2.

A complete class example:

public class ModifiedSQLServerDialect extends SQLServer2008Dialect {

public ModifiedSQLServerDialect () {

super();

registerColumnType( Types.TIMESTAMP, "datetime" );

}

}

If you look at the source of SQLServer2008Dialect (assuming Hibernate 4.3), you may see that the Types.TIMESTAMP is mapped to "datetime2", we are effectively redefining it, in our own ModifiedSQLServerDialect.

Using the new dialect

To use the new dialect, we have just defined, you need to add the following property to the persistence.xml file:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值