hibernateの地図<key, set<value="">>
-
25-09-2019 - |
質問
私は、以下のテーブル:
@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
うに state
クラスの取得 map<key, set<value>>
の Map<Event, set<StateEventAction>>
どうしたらよいでHibernate?
解決
あなたがセットの地図を受け取るしたい場合は、
は、それが各(STATE_ID、EVENT_ID)アクションのいくつかのアクションが存在することを意味します。だから、間違ったエンティティのマッピングを持っています。それがあるべき
@Entity
@Table(name = "state_event_action")
StateEventAction
--id
--state_id
--event_id
--Set<action> actions
あなたが書くことができます。この場合、
@Entity @Table(name = "state")
State
--id
--name
Map<Event,StateEventAction> eventActions;
他のヒント
あなたはおそらく最初のクエリに必要なすべてのStateEventActionは、すでにその後、作成されていない場合は、最初のセットにStateEventActionのobejctを追加イベントのためにセットを作成するために、独自のコードを記述するためのオブジェクトの状態。
State state = // < the state object;
Query q = Session.createQuery("from StateEventAction sea inner join fetch sea.event where sea.state = :state");
q.setEntity("state", state);
Map<Event, Set<StateEventAction>> map = new HashMap<Event, Set<StateEventAction>>();
for(Iterator itr = q.list().iterator(); itr.hasNext();) {
StateEventAction sea = itr.next();
Event event = sea.getEvent();
Set<StateEventAction> theSet = map.get(event);
if(theSet == null) {
theSet = new HashSet<StateEventAction>();
map.put(event, theSet);
}
theSet.add(sea);
}
所属していません StackOverflow