oracle Hibernate 将两个表映射到一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1667918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Mapping Two Tables to One Class
提问by Nicholas Hirras
I need to map two tables to a single class, having trouble figuring this out. One table is ROOMS, the other is TRAINERS.
我需要将两个表映射到一个类,但无法解决这个问题。一张桌子是房间,另一张桌子是教练。
The ROOMS table:
房间表:
OOC_UNIT_ID NUMBER(6,0)
OOC_START_DT DATE
OOC_START_TM DATE
OOC_DT_MOD DATE
OOC_USER_MOD VARCHAR2(30 BYTE)
OOC_END_DT DATE
OOC_END_TM DATE
OOC_REASON_TX VARCHAR2(250 BYTE)
OOC_RESERVED_FOR VARCHAR2(30 BYTE)
OOC_CLS_ID NUMBER(9,0)
OOC_TIMEFRAME VARCHAR2(1 BYTE)
OOC_WSD_CD VARCHAR2(2 BYTE)
OOC_TEAM_UNIT_ID NUMBER(6,0)
OOC_WSD_ACT_RMAT_ID NUMBER(6,0)
TRAINERS table:
培训师表:
TRSC_ID NUMBER(9,0) -- generated sequence
TRSC_OOC_UNIT_ID NUMBER(6,0)
TRSC_OOC_START_DT DATE
TRSC_OOC_START_TM DATE
TRSC_OOC_RESERVED_FOR VARCHAR2(30 BYTE)
TRSC_TPOC_ID NUMBER(6,0)
TRSC_DT_CREATED DATE
TRSC_USER_CREATED VARCHAR2(30 BYTE)
TRSC_DT_MOD DATE
TRSC_USER_MOD VARCHAR2(30 BYTE)
TRSC_REMARKS VARCHAR2(250 BYTE)
TRSC_NOSHOW_REASON VARCHAR2(100 BYTE)
Tables should be joined on OOC_UNIT_ID=TRSC_OOC_UNIT_ID
, OOC_START_DT=TRSC_OOC_START_DT
and OOC_START_TM=TRSC_OOC_START_TM
.
表应在OOC_UNIT_ID=TRSC_OOC_UNIT_ID
、OOC_START_DT=TRSC_OOC_START_DT
和 上连接OOC_START_TM=TRSC_OOC_START_TM
。
Primary Key for ROOMS table is: OOC_UNIT_ID, OOC_START_DT, OOC_START_TM
. Primary Key for the TRAINERS table is: TRSC_ID
.
ROOMS 表的主键是:OOC_UNIT_ID, OOC_START_DT, OOC_START_TM
。TRAINERS 表的主键是:TRSC_ID
。
I need to query this data by OOC_UNIT_ID
, OOC_START_DT
, OOC_START_TM
, OOC_END_DT
, OOC_END_TM
and OOC_WSD_ACT_RMAT_ID
.
我需要通过查询这个数据OOC_UNIT_ID
,OOC_START_DT
,OOC_START_TM
,OOC_END_DT
,OOC_END_TM
和OOC_WSD_ACT_RMAT_ID
。
In SQL it might be something like:
在 SQL 中,它可能类似于:
SELECT *
FROM TRAINERS t, ROOMS r
WHERE t.TRSC_OOC_UNIT_ID = r.OOC_UNIT_ID
AND t.TRSC_OOC_START_DT = r.OOC_START_DT
AND t.TRSC_OOC_START_TM = r.OOC_START_TM
AND ...
I am using the ROOMS table elsewhere in the project and it is already mapped as a standalone object. Would there be a way to utilize that as a sub-object on a TRAINERS object, or would it be easier to map these two tables into one flat object? How would the mapping look?
我在项目的其他地方使用了 ROOMS 表,它已经被映射为一个独立的对象。有没有办法将其用作 TRAINERS 对象上的子对象,还是将这两个表映射到一个平面对象会更容易?映射看起来如何?
Thanks, Nick
谢谢,尼克
回答by ChssPly76
To map a single class to two (or more) separate tables you need to use a @SecondaryTableannotation:
要将单个类映射到两个(或多个)单独的表,您需要使用@SecondaryTable注释:
@Table(name="ROOMS")
@SecondaryTable(name="TRAINERS", pkJoinColumns={
@PrimaryKeyJoinColumn(name="TRSC_OOC_UNIT_ID", referencedColumnName="OOC_UNIT_ID"),
@PrimaryKeyJoinColumn(name="TRSC_OOC_START_DT", referencedColumnName="OOC_START_DT"),
@PrimaryKeyJoinColumn(name="TRSC_OOC_START_TM", referencedColumnName="OOC_START_TM")
})
public class MyMergedEntity {
You'll then need to annotate each individual property mapped to TRAINERS
table with @Column(table="TRAINERS")
to specify which table it belongs to. If you're using XML mappings instead, all of the above can be done via joinelement.
然后,您需要注释映射到TRAINERS
表的每个单独属性,@Column(table="TRAINERS")
以指定它属于哪个表。如果您改用 XML 映射,则上述所有操作都可以通过join元素完成。
All that said, it seems to me that your two tables are rather different in nature and should not be mapped to a single class (especially since you've said you've already mapped ROOMS
elsewhere). Perhaps you should map your Trainer as ManyToOneassociation instead.
综上所述,在我看来,您的两个表在性质上相当不同,不应映射到单个类(特别是因为您已经说过您已经映射ROOMS
到其他地方)。也许您应该将您的 Trainer 映射为ManyToOne关联。
回答by Nicholas Hirras
Solution I've come up with seems to be working as far as querying data, I haven't tried any insert/update/delete yet.
我想出的解决方案似乎在查询数据方面有效,我还没有尝试过任何插入/更新/删除。
I created the TRAINER
object to extend the ROOM
object.
我创建了TRAINER
对象来扩展ROOM
对象。
public class Trainer extends Room {
...
}
Then I modified the mapping for ROOM
to include a joined-subclass:
然后我修改了映射ROOM
以包含一个加入的子类:
<hibernate-mapping>
<class name="Room" table="ROOMS">
<composite-id> ...
<property ...>
...
<joined-subclass name="Trainer" table="TRAINERS">
<key>
<column ...>
...
</key>
<property ...>
...
</joined-subclass>
</class>
...
So far it appears to be working.
到目前为止,它似乎正在起作用。
Thanks,
谢谢,
Nick
缺口
回答by monksy
You can create a named query that is binded to a custom object. This is the solution I would go with, since no changes to the DB would be necessary.
您可以创建绑定到自定义对象的命名查询。这是我会采用的解决方案,因为不需要更改数据库。
回答by Tendayi Mawushe
In my experience simplicity is key when using any ORM including Hibernate. I would create a database view based on your SQL lets call that TRAINERS_ROOMS
then simple map that database view to a new Java object lets call that TrainersRooms
.
根据我的经验,在使用包括 Hibernate 在内的任何 ORM 时,简单性是关键。我会根据您的 SQL 创建一个数据库视图让我们调用它,TRAINERS_ROOMS
然后将该数据库视图简单映射到一个新的 Java 对象让我们调用它TrainersRooms
。
You get simple easy to manager hibernate mappings, but of course you can perform any updates using this new object so if you need that, this solution won't work out for you.
您可以轻松轻松地管理休眠映射,但当然您可以使用这个新对象执行任何更新,因此如果您需要,此解决方案将不适合您。