java - @ManyToMany with Hibernate and JPA -
i have 2 tables bound manytomany relationship.
table 1 timeslot , has collection of documents created in time slot. table 2 documents , has collection of timeslots given document modified many times in different timeslots.
in particular program each document find or create document row it. further find or create time slot represents epoch of action, create, update, delete. note: delete event performed on document, not on row represents it. in program there no deletes or removes performed against row of either table.
however seeing many deletes against timeslot_document mapping table there inserts.
my question this, why hibernate issuing deletes? appears related dirty update processing. why repeated deletes timeslot id=1?
when had onetomany didn't see deletes model inaccurate (as might imagine).
for every document determine date (hour accuracy only) action occured on, increment tally , add document collection.
something else curious implement mvcc via @version annotation yet in 'show innodb status\g' see table locked. @ moment there 1 row timeslot, 1988 documents tallied in , version of row @ 1999. table locking concerning me.
here main code fragment:
(eventlog event : eventslist) { currentevent = event.geteventid(); string tmp = formatter.format(event.getepoch()); date epoch = null; try { epoch = formatter.parse(tmp); } catch (parseexception ex) { logger.getlogger(converter.class.getname()).log(level.severe, null, ex); } cal.settime(epoch); tuple tuple = holding.get(epoch); if (tuple == null) { tuple = new tuple(); holding.put(epoch, tuple); } querytimeslot.setparameter("doctype", doctype); querytimeslot.setparameter("date", epoch); try { ats = (timeslot)querytimeslot.getsingleresult(); insertats = false; } catch (exception e) { insertats = true; ats = new timeslot(); atsk = new timeslotkey(); atsk.setslotdate(epoch); atsk.setdoctype(doctype); ats.sethour(cal.get(calendar.hour_of_day)); ats.setslotkey(atsk); } if (processmm) { querydocument.setparameter("id", event.getupdeventlogdocid()); querydocument.setparameter("doc", doctype); try { doc = (document)querydocument.getsingleresult(); insertdoc = false; } catch (exception e) { doc = new document(); dockey = new documentkey(); dockey.setdocid(event.getupdeventlogdocid()); dockey.settype(doctype); doc.setdockey(dockey); insertdoc = true; } } try { ats.getdocuments().add(doc); } catch (exception e) { logger.getlogger(converter.class.getname()).log(level.severe, "{0}", e); system.exit(-1); } if (processmm) { doc.gettimes().add(ats); } timeslot entity
@entity @table(name = "timeslot", catalog = "analytics") public class timeslot implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue @basic(optional=false) @column(name="id") private integer id; @basic(optional = false) @column(name = "slotkey") private timeslotkey slotkey; @basic(optional = false) @column(name = "slothour") private int slothour; @basic(optional = false) @column(name = "inserts") private int inserts; @basic(optional = false) @column(name = "deletes") private int deletes; @basic(optional = false) @column(name = "active") private int active; @manytomany(fetch=fetchtype.lazy) @joincolumn(name="doc_id") private collection<document> documents; @basic(optional = false) @version @column(name = "version") private int version; public timeslot() { documents = new arraylist<document>(0); } public collection<document> getdocuments() { return documents; } public void setdocuments(collection<document>documents) { this.documents = documents; } public int gethour() { return slothour; } public void sethour(int slothour) { this.slothour = slothour; } @override public int hashcode() { int hash = 0; hash += (slotkey != null ? slotkey.hashcode() : 0); return hash; } @override public boolean equals(object object) { if (!(object instanceof activitytimeslot)) { return false; } } documents follows:
@entity @table(name = "documents", catalog = "analytics") public class document implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue @basic(optional=false) @column(name="id") private integer id; @basic(optional = false) @column(name = "dockey") private documentkey dockey; @basic(optional = false) @column(name = "inserts") private int inserts; @basic(optional = false) @column(name = "deletes") private int deletes; @manytomany(fetch=fetchtype.lazy, mappedby="") @joincolumn(name="slot_id") private collection<timeslot> times; @basic(optional = false) @version @column(name = "version") private int version; public document() { times = new arraylist<timeslot>(0); inserts = 0; deletes = 0; } @override public int hashcode() { return dockey.hashcode(); } @override public boolean equals(object object) { return dockey.equals(object); } public int getdeletes() { return deletes; } public void setdeletes(int deletes) { this.deletes = deletes; } public void incrdeletes() { deletes++; } public void incrinserts() { inserts++; } public int getinserts() { return inserts; } public void setinserts(int inserts) { this.inserts = inserts; } public pair getpair() { return new pair(inserts, deletes); } @override public string tostring() { return dockey.tostring(); } public collection<activitytimeslot> gettimes() { return times; } public void settimes(collection<activitytimeslot>times) { this.times = times; } public documentkey getdockey() { return dockey; } public void setdockey(documentkey dockey) { this.dockey = dockey; } public integer getid() { return id; } public void setid(integer id) { this.id = id; } } new info
i happened across manytomany assoicate delete join table entry
which identical. it's don't understand arthur's answer. noticed in mysql innodb status doing outer left join...is necessary?
so in entitya should have method add(entityb) , in entityb should have method called add(entitya) in these methods
public void add(entityb entity) { entity.getlist().add(this); getlist().add(entity); } i not seeing how functionally different have.
Comments
Post a Comment