Hibernate:在多对一类映射中设置默认值
我正在尝试将语言类映射到用户类。
多对一映射工作正常,通过应用程序的功能,我可以将值从默认值 null 设置为特定值。
问题是我想将默认映射设置为 idLanguage 1,但是当它映射时,所有内容都设置为 null。
我已经尝试过:
< many-to-one name="language" class="com. \etc\ .language.Language" column="language" default="1" lazy="false" />
但这给了我一个错误“必须为元素类型多对一声明属性默认值。
该怎么办?
(注意:语言映射到用户类中的语言对象,而不是语言 ID。应用程序中的设置将语言的 id 放入语言列中,替换空值)
I am trying to map a class Language to a class User.
Many to one mapping is working fine, and through the functionality of the app I can set the value from it's default of null to a particular value.
The problem is that I'd like to set the default mapping to idLanguage 1, but when it maps everything is set to null.
I have tried:
< many-to-one name="language" class="com. \etc\ .language.Language" column="language" default="1" lazy="false" />
However that gives me an error "Attribute default must be declared for element type Many To One.
What to do?
(note: language is mapped to language object in user class, rather than a language id. Setting in the app puts the id of the language in the language column, replacing null value)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望这些语言将成为您应用程序中的种子数据。然后在 User 映射中有一个单独的 langId 字段并将其设置为 1。加载 User bean 时可以使用对象映射。因此请按如下所示修改映射。
这样,它的性能会更高,因为在保存用户 bean 之前,您不必从数据库中获取语言 bean。此外,在用户加载时,语言也将可供使用。
I hope the languages will be seed data in your application. Then have a separate langId field in the User mapping and set it to 1. The Object mapping can be used while loading the User bean.So modify the mapping as below.
This way it will be more performative as you don't have to fetch the Languages bean from the DB before you save the User bean. Also on load of User the languages will be available to.