java - Hibernate and @ElementCollection: Duplicate Key -
i have cronjob running, calculating lot of stuff once day.
there 2 entites involved.
first 1 "kpi" entity, containing information user activities. every kpi entity 1:1 related user ofc. kpi entity related 1:n more detailed drill down. (it's element collection inside kpi entity).
simpliefied:
@entity @table(name = dbconstants.kpi_table_name) @namedqueries({ @namedquery(name = "kpi.byuser", query = "select k kpientity k k.user = :user"), @namedquery(name = "kpi.all", query = "select k kpientity k"), @namedquery(name = "kpi.allactiveusers", query = "select k kpientity k k.user.accountactive = :accountstatus") }) public class kpientity implements serializable { @id @generatedvalue(strategy = generationtype.identity) private int id; @column private long taskscompleted; @elementcollection(targetclass = java.lang.long.class, fetch = fetchtype.eager) @mapkeyclass(java.lang.string.class) @mapkeycolumn(name = "taskkey") @collectiontable(name = "kpitaskdrilldown", joincolumns = @joincolumn(name = "id")) @column(name = "count") private map<string, long> taskscompletedpertype; }
so, assume hibernate creates 2 tables:
"kpis":
id | taskscompleted 1 | 250 2 | 50
kpitaskdrilldown
kpi_id | taskscompletedpertype | count 1 | | 20 1 | b | 75 ...
this "works" - first run. if cronjob runs again, receive duplicate-key-mysql-errors.
i noticed, hibernate has created composite key on drilldown table: kpi_id, taskscompletedpertype
. seems fine far.
but if update on kpi entity, hibernate seems try insert values second table - fail due constraint.
any ideas?
Comments
Post a Comment