简体   繁体   English

休眠二级无方向映射

[英]Hibernate second level undirectional mapping

I do not know if I explained myself in question, but basically I want to know if there is some functionality to map object that is not in direct relation with the target. 我不知道我是否在解释自己,但基本上我想知道是否存在某些功能来映射与目标没有直接关系的对象。 For example: 例如:

|Table1   |   |Table2   |   |Table3   |
-----------   -----------   ----------
|id       |   |id       |   |id       |
|someValue|   |Table1_fk|   |Table2_fk|

and I could access Table2 object in class relation like this: 并且我可以像这样在类关系中访问Table2对象:

public class Table1 {
@Id
int id;
@Column
String someValue;
@OneToOne(mappedBy="Table2")
Table2 table2;
}

So my question is - can I map from Table1 to Table3? 所以我的问题是-我可以从Table1映射到Table3吗?

Given your above code, it doesn't really sound like you need to "map", or even establish a relationship to, Table3. 鉴于上面的代码,这听起来好像并不需要“映射” Table3,甚至不需要建立与Table3的关系。

Do you need properties from Table3 to just be available as part of Table1's API contract? 您是否需要Table3的属性才能作为Table1的API合同的一部分使用? If so, you can do something like: 如果是这样,您可以执行以下操作:

public class Table1 {
    @Id
    int id;

    @Column
    String someValue;

    @OneToOne(mappedBy="Table2")
    Table2 table2;

    public Table3 getTable3() {
        return table2.getTable3();
    }
}

This assumes that the relationship is established in Table3, and 'mappedBy' in Table2, which is the sensible mapping given that Table3 actually contains the foreign key. 假定该关系在表3中建立,在表2中建立“ mappedBy”,鉴于表3实际上包含外键,这是明智的映射。

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

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