Search code examples
javahibernatehibernate-mappingpojo

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


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.


Solution

  • 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>