Alex
Alex

Reputation: 2146

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>();

Upvotes: 0

Views: 481

Answers (2)

bvanvelsen - ACA Group
bvanvelsen - ACA Group

Reputation: 1751

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>();

Upvotes: 1

axtavt
axtavt

Reputation: 242686

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.

Upvotes: 1

Related Questions