使用datetime代替datetime2 sqlserver休眠 [英] use datetime instead of datetime2 sqlserver hibernate
问题描述
我已将休眠配置为自动更新。我有一个mssql-server2012。Hibernate在数据库中将我的java.util.Date对象创建为datetime2。是否有可能迫使休眠模式在数据库中创建日期时间?
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?
我知道我可以在对象的字段上使用注释,但是注释太多了。因此,我想配置休眠模式,以便每个java.util.Date都自动成为数据库中的日期时间。
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.
推荐答案
第一个我想到的是定义一个新的Hibernate Dialect
,它将Java数据类型映射到 datetime
,而不是默认 datetime2
。
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
.
完整的类示例:
public class ModifiedSQLServerDialect extends SQLServer2008Dialect {
public ModifiedSQLServerDialect () {
super();
registerColumnType( Types.TIMESTAMP, "datetime" );
}
}
如果您查看<$ c $的来源c> SQLServer2008Dialect (假设为Hibernate 4.3),您可能会看到 Types.TIMESTAMP
映射到 datetime2
,我们正在使用我们自己的 ModifiedSQLServerDialect
重新定义它。
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
.
新的方言
要使用新的方言,我们已经定义好了,您需要将以下属性添加到 persistence中。 xml
文件:
To use the new dialect, we have just defined, you need to add the following property to the persistence.xml
file:
<property name="hibernate.dialect" value="com.example.ModifiedSQLServerDialect" />
这篇关于使用datetime代替datetime2 sqlserver休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!