简体   繁体   中英

mapping a map JPA, hibernate key

How can I map a map?

I have this:

private Map<Integer, Trip> trips = new HashMap<Integer, Trip>();

As mapkey I want the trip id..

The Trip entity is holding the reference to my entity. (the column is in another table). The trip has a column to my reference id.

How can I do that?

Something like

@MapKeyTable(name="trips")
@MapKeyColumn(name="trip_id")
@OneToMany(joinTable......)
private Map<Integer, Trip> trips = new HashMap<Integer, Trip>();

If trip id is a property of Trip (say tripId ), then you need the following:

@MapKey(name = "tripId")
@OneToMany(...)
private Map<Integer, Trip> trips = new HashMap<Integer, Trip>();

Other annotations are for more complex cases.

Something like this:

@OneToMany(cascade = CascadeType.PERSIST)
@JoinTable(name = "jointableName", joinColumns = @JoinColumn(name = "this_id"), inverseJoinColumns = @JoinColumn(name = "trip_id"))
private Map<Integer, Trip> trips= new Hashtable<Integer, Trip>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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