Re[2]: Hibernate mapping Map
От: merzod  
Дата: 16.08.11 08:02
Оценка:
Спустя время вернулся к этой же проблеме. Сделал иначе
Все свел к 2м таблицам: customer(id, name), settings(id, customer_id, name, value)
Ввел enum для удобного доступа к пропертям.
Метод getSettings() в Customer'e аннотирован следующим образом:
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = Settings.class) // OneToMany - map value is entity, targetEntity - map value isn't generic type
@JoinColumn(name = "customer_id") // join
@MapKey(name = "name") // map key - property of the entity
public Map<Props, Settings> getSettings() {...}

Добавил врап-методы и теперь просто и удобно работать c настройками:
String name = customer.getSetting(Props.name).getValue();
int type = customer.getSetting(Props.type).getIntValue();
customer.getSetting(Props.type).setValue(1);
...
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.