I have the following tables:
@Entity
@Table(name = "events")
Event
--id
--name
@Entity
@Table(name = "state")
State
--id
--name
@Entity
@Table(name = "action")
Action
--id
--name
@Entity
@Table(name = "state_event_action")
StateEventAction
--id
--state_id
--event_id
--action_id
I would like in state class to get map<key, set<value>> of Map<Event, set<StateEventAction>>
How can I do it in Hibernate?
I would like in state class to get
map<key, Set<value>>ofMap<Event, Set<StateEventAction>>
Hibernate does not support collection of collections such as List of Lists, Map of Sets, etc out of the box. But you could implement your own UserCollectionType to add support for this kind of data structure. This blog post shows how to do so using the MultiMap implementation from Apache commons.
My suggestion would be to use a similar approach, but maybe to prefer the generified Multimap from Google Guava.
If you want to recieve Map of sets, it means that there are several actions for each (state_id, event_id) actions. So you have wrong entity's mappings. It should be
@Entity
@Table(name = "state_event_action")
StateEventAction
--id
--state_id
--event_id
--Set<action> actions
In this case you can write :
@Entity @Table(name = "state")
State
--id
--name
Map<Event,StateEventAction> eventActions;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With