一对多映射//如何映射遗留表的关联(Hibernate) [英] one-to-many mapping // how to map an association of legacy tables (Hibernate)

查看:113
本文介绍了一对多映射//如何映射遗留表的关联(Hibernate)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个遗留数据库表格,简化了这种方式:

I've got two legacy database tables, layouted in simplified manner like this:

MASTER                        SLAVE
ident                         ident
sName                         sName
sNumber                       sNumber
sDesc                         sValue
-----                         ------
Java Class 'ScenarioMaster'   Java Class 'ScenarioSlave'

每行都有通过 ident 列的代理索引,但在 MASTER.key SLAVE.key 之间没有关系。但是,对于 MASTER 表,一个sName / sNumber对的唯一性为true。因此,这将是一个可能有意义的复合键(我知道那些是坏的)。

Each row has a surrogate index via the ident column, but there is no relationship between MASTER.key and SLAVE.key. However, for the MASTER table, uniqueness of one sName/sNumber pair is true. This would therefore be a possible and meaningful composite key (I do know that those are evil).

事实上,有一个1:n关系意味着每个 MASTER 行引用 n SLAVE 表中的行。根据上面的列描述,可能的人口可能是

In fact, there is a 1:n relationship meaning each MASTER row references n rows in the SLAVE table. Given the column description from above, possible population could be

MASTER                        SLAVE
100                           42
'labl'                        'labl'
1                             1
'some label'                  0.1
                              43
                             'labl'
                              1
                              0.2

现在,使用hibernate,我如何在我的java类中反映这种关系?在 ScenarioMaster 中,我将使用公共getters / setter声明一个Set / List,如

Now, using hibernate, how could I reflect this relationship in my java classes? In ScenarioMaster I would declare a Set or List with public getters/setters like

private List<ScenarioSlave> slaves = new ArrayList<ScenarioSlave>();

ScenarioMaster 的hibernate映射可能包含

The hibernate-mappings for ScenarioMaster could contain

<bag name="slaves" cascade="all">
    <key>
        <column name="sName" not-null="true"/>
        <column name="sNumber" not-null="true"/>
    </key>
    <one-to-many class="ScenarioSlave"/>
</bag>

然而,在更新已经存在的 ScenarioMaster时,会创建一个strage update语句实体使用 session.saveOrUpdate(scenarioMaster)

This, however, creates a strage update statement when updating an already persitant ScenarioMaster entity using session.saveOrUpdate(scenarioMaster).

// create master scn and slave slv      
scn.addSlave(slv);  
session.saveOrUpdate(scn);

    Hibernate: 
        update
            SLAVE
        set
            sNumber=?,
            sName=?,
            sValue=?
        where
            sName=? 
            and sNumber=?
    Hibernate: 
        update
            SLAVE
        set
            sName=null,
            sNumber=null 
        where
            sName=? 
            and sNumber=?   

我做错了什么?第二次更新来自哪里?
我想这与sName / sNumber有关,不是 ScenarioSlave 的关键。我不明白为什么。

What am I doing wrong? Where is that second update coming from? I guess it has something to do with sName/sNumber not being a key for ScenarioSlave. I can't quite figure out, why.

请注意,sName / sNumber参数确实指向有效值,而且我想要持续的 ScenarioMaster 实例 saveOrUpdate 奴隶中实际上有一个非空值 ScenarioSlave 实例列表。

Note that the sName/sNumber parameters do point to valid values and that the ScenarioMaster instance I want to persist via saveOrUpdate actually has a non-null ScenarioSlave instance in the slaves List.

编辑:使用此映射将复合键推迟到单独的类

EDIT: Composite key is deferred to a separate class using this mapping

<composite-id name="keyId" class="ScenarioKeyId">
    <key-property name="name" access="field" column="sName"
        type="string" length="20"/>
    <key-property name="number" access="field" column="sNumber"
        type="long"/>
</composite-id>

我真的不想创建一个表 NAMENUMBER_KEY 'labl',1 映射到像'key_labl1'之类的东西,然后可以用作 id 用于休眠我想,这是hibernate做的(不使用实际的表)。

I don't really want to create a table NAMENUMBER_KEY which maps 'labl', 1 to something like 'key_labl1' which can then be used as id for hibernate. I suppose, this is what hibernate does (without making use of an actual table).

推荐答案

Hibernate对复合不能很好钥匙,在我的经验。这是Hibernate的一个主要问题,就我所见。过去,我已经解决了它,避免使用纯粹的复合键;也就是说,我给复合键表一个唯一的ID列,Hibernate然后可以工作。

Hibernate does not work well with composite keys, in my experience. It's the one main problem with Hibernate as far as I've seen. I've worked around it in the past by avoiding the use of "purely" composite keys; that is, I've given composite key tables a unique ID column which Hibernate can then work off of.

这篇关于一对多映射//如何映射遗留表的关联(Hibernate)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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