Hibernate - XML Mapping of a N<->N Table with extra columns

不羁的心 提交于 2020-01-06 07:43:26

问题


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:

Map<Route, Double> customRoutesPrices;

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

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

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

Set<CustomRoute> customRoutes;

which may be the set of CustomRoutes for that Customer.

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

Thank you in advance.


回答1:


You can declare a Map<Route,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:


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



来源:https://stackoverflow.com/questions/7179538/hibernate-xml-mapping-of-a-n-n-table-with-extra-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!