Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate map<key, set<value>>

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?

like image 371
Dejell Avatar asked Jan 25 '26 05:01

Dejell


2 Answers

I would like in state class to get map<key, Set<value>> of Map<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.

like image 190
Pascal Thivent Avatar answered Jan 27 '26 18:01

Pascal Thivent


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;
like image 37
Igor Grinfeld Avatar answered Jan 27 '26 17:01

Igor Grinfeld



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!