简体   繁体   English

休眠:在多对一类映射中设置默认值

[英]Hibernate: Set a default in many-to-one class mapping

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. 多对一映射工作正常,通过应用程序的功能,我可以将值从默认的null设置为特定值。

The problem is that I'd like to set the default mapping to idLanguage 1, but when it maps everything is set to null. 问题是我想将默认映射设置为idLanguage 1,但是当它映射所有内容时都设置为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) (注意:语言映射到用户类中的语言对象,而不是语言ID。在应用程序中进行设置会将语言的ID放入“语言”列中,替换为空值)

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. 然后在User映射中有一个单独的langId字段并将其设置为1。可以在加载User Bean时使用Object映射。因此,请如下修改映射。

< many-to-one name="language" class="com. \etc\ .language.Language" column="language" lazy="false" insert="false" update="false"/>

<property name="langId" column="language"/>

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. 这样一来,它将更具性能,因为在保存User Bean之前不必从数据库中获取Languages Bean。 Also on load of User the languages will be available to. 同样,在用户加载时,语言也将可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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