从休眠到mysql,默认值映射问题 [英] from hibernate to mysql, default value mapping issue

查看:71
本文介绍了从休眠到mysql,默认值映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

休眠xml映射文件不支持默认值.因此,当我尝试在mysql中创建具有默认值的列时,然后我运行hibernate save()方法而不设置列值.默认值无法生成.

hibernate xml mapping file does not support default value. So when i try to create a column with default value in mysql, then i run the hibernate save() method without setting the column value. the default value cannot be generated.

我已经做了一些搜索.根据这篇文章: https://forums.hibernate.org/viewtopic .php?t = 171& highlight = default + value& sid = 84a014fd93dd9b680afc606f616ca4f6 它说休眠不支持默认值.我们可以使用触发器代替.

i have done some search. according to this post: https://forums.hibernate.org/viewtopic.php?t=171&highlight=default+value&sid=84a014fd93dd9b680afc606f616ca4f6 it said hibernate does not support default value. we can use trigger instead.

除触发器之外,我们能采取其他任何方式吗?以及如何通过触发来做到这一点

Any way we can do this except trigger? and how can we do this by trigger

好吧,我尝试进行更多搜索,我找到了答案:

OK i try to do more search i find the answer:

ibernate不支持默认值,您应该使用数据库触发器来管理此值,或者在您的域中分配默认值.

ibernate does not support default values, you should either manager this using database triggers or assign default values in your domain.

但是,如果我在自己的域中分配默认值,那么在构造函数中说,我们有什么缺点吗?

But if i assign default values in my domain, said in constructer, do we have any drawback?

thx!

推荐答案

我建议使用构造函数来设置对象的默认状态.休眠是一个ORM.基本原则是保存对象.通过在DBMS端使用默认值,可以将对象保存为无效状态,从而破坏了整个概念. (通过使字段值可以跳过 setter 方法).

I suggest using the constructor to set up the default state of your objects. Hibernate is an ORM. The basic philosophy is that you save your objects. Using default values at the DBMS end can undermine the whole concept, by making it possible to save objects in invalid states. (By making possible for field values to skip the setter methods).

另一方面,当实例化一个对象时,它的字段已经初始化(为空值或零或空字符串等).对于休眠来说,Hibernate不可能猜测您是否要保存实际的空值或零.想要改用数据库的默认值.

In the other hand, when you instantiate an object its fields are already initiated (to null value or zero or empty string etc.) It would be impossible for Hibernate to guess whether you want to save the actual null or zero or you want to use the DB's default value instead.

当您在谈论DBMS级别的默认值时,我假设您想到的是原始值.在Java端初始化原始字段(在构造函数内部或通过声明)绝对没有缺点.

When you're talking about default values in the DBMS level, i assume you think of primitive values. There is absolutely no drawback in initializing primitive fields on the Java side (inside the constructor or by declaration).

这篇关于从休眠到mysql,默认值映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆