Hibernate - 带有额外列的N N表的XML映射

时间:2022-11-30 16:57:02

I have this schema:

我有这个架构:

---------------  --------------------  ----------------
| Customers   |  | CustomRoutePrice |  | Route        |
|-------------|  |------------------|  ---------------|
| CustId (pk) |  | CustId  (pk)     |  | RouteId (pk) |
| Desc        |  | RouteId (pk)     |  | Desc         |
---------------  | Price            |  | Price        |
                 --------------------  ----------------

and I want to map the CustomRoutePrice's Price to my Customers POJO, saying something like:

我想将CustomRoutePrice的价格映射到我的客户POJO,说如下:

Map<Route, Double> customRoutesPrices;

or maybe having a new POJO called CustomRoute, so it may look something like this:

或者可能有一个名为CustomRoute的新POJO,所以看起来像这样:

public class CustomRoute {
    private Customer customer;
    private Route route;
    private Double price;
}

so within my Customers POJO I could have a set like:

所以在我的客户POJO中,我可以有一套如下:

Set<CustomRoute> customRoutes;

which may be the set of CustomRoutes for that Customer.

这可能是该客户的CustomRoutes集合。

So my question is how can I make possible both mappings?

所以我的问题是如何才能使这两种映射成为可能呢?

Thank you in advance.

先感谢您。

2 个解决方案

#1


0  

You can declare a Map<Route,Double>:

您可以声明Map : ,double>

<map name="customRoutesPrices" table="CustomRoutePrice">
  <key column="CustId" not-null="true"/>
  <map-key-many-to-many class="Route" column="RouteId"/>
  <element column="Price" type="double"/>
</map>

#2


0  

It is the classical example of many-to-many mapping where the association table has extra columns besides the PK of both many sides. Here shows a complete example for this type of mapping

它是多对多映射的经典示例,其中关联表除了多个方面的PK之外还有额外的列。这里显示了此类映射的完整示例

#1


0  

You can declare a Map<Route,Double>:

您可以声明Map : ,double>

<map name="customRoutesPrices" table="CustomRoutePrice">
  <key column="CustId" not-null="true"/>
  <map-key-many-to-many class="Route" column="RouteId"/>
  <element column="Price" type="double"/>
</map>

#2


0  

It is the classical example of many-to-many mapping where the association table has extra columns besides the PK of both many sides. Here shows a complete example for this type of mapping

它是多对多映射的经典示例,其中关联表除了多个方面的PK之外还有额外的列。这里显示了此类映射的完整示例