【问题标题】:Difference between one to one vs many to one with unique = true一对一与多对一之间的区别,唯一=真
【发布时间】:2014-11-03 14:31:12
【问题描述】:

hbm 文件中的两个标签如何与unique="true" 一对一和多对一不同? 我刚刚了解到,要获得双向的一对一映射,我需要在两个 hbm 文件上使用一对一的标签。我不能将多对一与unique="true" 放在两边(都是hbm)以获得休眠中的双向行为吗?

在这个link,我找到了

<!-- In One-to-one we cannot specify the foreign key column 
         that has to be filled up
        <one-to-one name="person" class="PersonOTO_B" cascade="all"
        constrained="true"> </one-to-one>
    -->

    <many-to-one name="person" column="P_ID" unique="true"
        not-null="true" lazy="false" />

上述理由是否成立?

问候 贾延德拉

【问题讨论】:

标签: hibernate mapping hibernate-mapping one-to-one associative


【解决方案1】:

如果您在应用程序启动时没有使用 Hibernate 生成 SQL 架构,unique = true 没有任何意义。 Hibernate 不强制值的唯一性,这是一个纯粹的数据库功能。

如果你使用 Hibernate 来生成你的数据库模式,那么它会在你设置了 unique=true 的任何属性上放置一个唯一约束,但是如果你将 Hibernate 指向一个已经存在的数据库,并且只验证那个模式数据库,Hibernate 不会检查唯一约束是否存在。

这适用的其他属性是(从我的头顶)insertableupdateablenullable

如果你想了解如何在 Hibernate 中创建不同类型的关系,我写了一个方便的指南,你可以找到 here。只需将我拥有的注释示例翻译到您的 .cfg 文件中,或者只使用注释即可。

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多