Hibernate,如何建模这种关系 [英] Hibernate, how to model this relationship

查看:184
本文介绍了Hibernate,如何建模这种关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  create table logical_id_seq(
logical_id int auto_increment,
primary key(logical_id)
);

create table mytable(
physical_id int auto_increment,
logical_id int not null引用parent(logical_id),
data varchar(20),
主键(physical_id)
);

第二个表格使用第一个表格自动生成的值作为其值。我不知道如何在休眠模型中进行建模。

我读过 http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-onetoone ,但我doesn看起来很难理解。

解决方案

实际上很难说,我不知道你想在对象中表达什么级别:它是一对一的外键关联吗?多对一的关联?协会是双向的吗?使用ORM意味着思考对象不仅仅是表格,它通常有助于提供对象模型。



我假设这是一对一的外键关联。以下是 Java与Hibernate的持久性建议:


7.1.2一对一外键关联



不用共享主键,两个
行可以有一个外键
关系。一个表具有引用相关表的主要
键的外部
键列。 (这个外键
约束的
源和目标甚至可以是同一个表:
这被称为自引用
关系。)


让我们更改从用户
到地址的映射。而不是共享的
主键,现在您在
USERS表中添加
SHIPPING_ADDRESS_ID列:

 < class name =Usertable =USERS> 
<多对一名称=shippingAddress
class =地址
column =SHIPPING_ADDRESS_ID
cascade =保存更新
独特= 真/>
< / class>

这个
关联的XML映射元素是<多对一> - 不是
<一对一> ,因为您可能有
预期。原因很简单:您
不关心目标端
的关联,因此您可以将
视为 to-one 关联而无需
的很多部分。所有你想要的是
表示这个实体有一个属性
是一个对
另一个实体的实例的引用,并且使用一个外键
来表示这个关系。
这个映射
的数据库模式如图7.3所示。




图7.3一对一外币
关键关联介于 USERS
ADDRESS
之间

一个额外的约束将这个
关系强制为一对一的真实关系。通过
使 SHIPPING_ADDRESS_ID
列唯一,您声明
特定地址可以被至多一个用户引用
,作为运输
地址。这不如从共享主键
关联中获得的
保证那么强大,这允许特定的
地址被最多
一个用户,句号引用。有几个外国
键列(假设您还有
独特 HOME_ADDRESS_ID
BILLING_ADDRESS_ID ),您可以
多次引用相同的地址目标行
。但无论如何,两位
用户不能共享同一个地址作为
用于同一目的。



让我们从User

$ b

逆向属性引用



最后一个外键关联是
映射从用户到地址,以
<多对一> 和一个唯一的
约束来确保所需的
多重性。什么映射元素可以在地址端添加
以使
这个关联成为双向的,因此在Java域模型中可以从Address到User访问的



在XML中,使用属性引用$ b创建< one-to-one>
映射$ b属性:

 <一对一名称=user
class =User
property-ref =shippingAddress/>

您告诉Hibernate,Address类的用户
属性是
该关联的另一
一侧的财产的逆转。您现在可以通过
调用 anAddress.getUser()来访问给出
的送货地址的用户
。没有额外的列
或外键约束; Hibernate
为你管理这个指针。


如果你真的有一个真正的多对一关联,应该很容易适应上述解决方案。


I have the below tables.

create table logical_id_seq (
    logical_id int auto_increment,
    primary key(logical_id)
);

create table mytable (
    physical_id int auto_increment,
    logical_id int not null references parent(logical_id),
    data varchar(20),
    primary key(physical_id)
);

The second table uses first table auto-generated value as its value. I am not sure how to model this in hibernate.

I read http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-onetoone, but I doesn't seem to understand.

解决方案

It's actually hard to say, I don't know what you want to represent at the object level: is it a one-to-one foreign key association? a many-to-one association? is the association bi-directional? Using an ORM means thinking objects more than tables and it usually help to provide the object model.

I'll assume this is a one-to-one foreign key association. Here is what Java Persistence with Hibernate recommends:

7.1.2 One-to-one foreign key associations

Instead of sharing a primary key, two rows can have a foreign key relationship. One table has a foreign key column that references the primary key of the associated table. (The source and target of this foreign key constraint can even be the same table: This is called a self-referencing relationship.)

Let’s change the mapping from a User to an Address. Instead of the shared primary key, you now add a SHIPPING_ADDRESS_ID column in the USERS table:

<class name="User" table="USERS">
  <many-to-one name="shippingAddress"
               class="Address"
               column="SHIPPING_ADDRESS_ID"
               cascade="save-update"
               unique="true"/>
</class>

The mapping element in XML for this association is <many-to-one> — not <one-to-one>, as you might have expected. The reason is simple: You don’t care what’s on the target side of the association, so you can treat it like a to-one association without the many part. All you want is to express "This entity has a property that is a reference to an instance of another entity" and use a foreign key field to represent that relationship. The database schema for this mapping is shown in figure 7.3.

Figure 7.3 A one-to-one foreign key association between USERS and ADDRESS

An additional constraint enforces this relationship as a real one to one. By making the SHIPPING_ADDRESS_ID column unique, you declare that a particular address can be referenced by at most one user, as a shipping address. This isn’t as strong as the guarantee from a shared primary key association, which allows a particular address to be referenced by at most one user, period. With several foreign key columns (let’s say you also have unique HOME_ADDRESS_ID and BILLING_ADDRESS_ID), you can reference the same address target row several times. But in any case, two users can’t share the same address for the same purpose.

Let’s make the association from User to Address bidirectional.

Inverse property reference

The last foreign key association was mapped from User to Address with <many-to-one> and a unique constraint to guarantee the desired multiplicity. What mapping element can you add on the Address side to make this association bidirectional, so that access from Address to User is possible in the Java domain model?

In XML, you create a <one-to-one> mapping with a property reference attribute:

<one-to-one name="user"
            class="User"
            property-ref="shippingAddress"/>

You tell Hibernate that the user property of the Address class is the inverse of a property on the other side of the association. You can now call anAddress.getUser() to access the user who’s shipping address you’ve given. There is no additional column or foreign key constraint; Hibernate manages this pointer for you.

If what you have is actually a real many-to-one association, it should be pretty easy to adapt the above solution.

这篇关于Hibernate,如何建模这种关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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