问题
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 Customer
s 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 Customer
s POJO I could have a set like:
Set<CustomRoute> customRoutes;
which may be the set of CustomRoute
s 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