Very Weird, Triplicated childs at runtime, all ok in the DB
raistlinmolina Sep 21, 2006 5:05 AMHello, I have (among other entities) one parent Entity called reportInstance and one child Entity Called reportParameterValue, the relation is OneToMany.
I have a strange problem that arised becauce I couldn't delete a parent object, when I tired to delete it's child I couldn't either.
Then I noticed that when I asked the parent for its childs I got them triplicated, all is ok in the database (childs not triplicated).
The child was a weak entity, having its PK formed by the PK of two tables, when I noticed this problem I tried:
*Adding an id filed as PK, now the child Entoty has it own pk (the former fileds forming the pk remain as Foreign Keys).
This didn't solve the problem, I couldn't delete anythimg and still got triplicated entries.
*Mapping the relationship in both sides, at first I had only mapped it at the Parent side as I didn't want to know a parent from the child.
This has solved the deletion problem but I still have triplicated childs!!!
I copy code snippets below:
The relation at ReportParameterValueEJB.java (the child)
@ManyToOne (targetEntity = ReportInstanceEJB.class, fetch = FetchType.EAGER) @JoinColumn (name = "idReportInstance", insertable=false, updatable=false) public ReportInstanceEJB getReportInstance(){ return this.reportInstance; } public void setReportInstance(ReportInstanceEJB reportInstance){ this.reportInstance = reportInstance; this.idReportInstance = reportInstance.getId(); }
The relation at ReportInstanceEJB.java (the parent)
/** * Returns the value of the <code>reportParameterValue</code> relation property. * * @return the value of the <code>reportParameterValue</code> relation property. */ @OneToMany(mappedBy="reportInstance", cascade= CascadeType.MERGE, fetch = FetchType.EAGER) public Collection <ReportParameterValueEJB> getReportParameterValues(){ return parameterValues; } /** * Sets the value of the <code>reportParameterValue</code> relation property. * * @param parameters a value for <code>reportParameterValue</code>. */ public void setReportParameterValues(Collection <ReportParameterValueEJB> parameters) { this.parameterValues = parameters; }
And now the traces I'm getting in the JBoss console:
10:36:57,612 INFO [STDOUT] Number of values:9 10:36:57,612 INFO [STDOUT] predelete: 10; 10; 2; 27 10:36:57,628 INFO [STDOUT] postdelete: 27 10:36:57,628 INFO [STDOUT] predelete: 10; 10; 2; 27 10:36:57,628 INFO [STDOUT] postdelete: 27 10:36:57,628 INFO [STDOUT] predelete: 10; 10; 2; 27 10:36:57,628 INFO [STDOUT] postdelete: 27 10:36:57,628 INFO [STDOUT] predelete: 12; 10; 2; 45 10:36:57,628 INFO [STDOUT] postdelete: 45 10:36:57,628 INFO [STDOUT] predelete: 12; 10; 2; 45 10:36:57,628 INFO [STDOUT] postdelete: 45 10:36:57,628 INFO [STDOUT] predelete: 12; 10; 2; 45 10:36:57,628 INFO [STDOUT] postdelete: 45 10:36:57,628 INFO [STDOUT] predelete: 13; 10; 2; 45 10:36:57,628 INFO [STDOUT] postdelete: 45 10:36:57,628 INFO [STDOUT] predelete: 13; 10; 2; 45 10:36:57,628 INFO [STDOUT] postdelete: 45 10:36:57,628 INFO [STDOUT] predelete: 13; 10; 2; 45 10:36:57,628 INFO [STDOUT] postdelete: 45 ########################################## 10:42:59,471 INFO [STDOUT] Number of values:3 10:42:59,471 INFO [STDOUT] predelete: 16; 15; 2; 27 10:42:59,471 INFO [STDOUT] postdelete: 27 10:42:59,471 INFO [STDOUT] predelete: 16; 15; 2; 27 10:42:59,471 INFO [STDOUT] postdelete: 27 10:42:59,471 INFO [STDOUT] predelete: 16; 15; 2; 27 10:42:59,471 INFO [STDOUT] postdelete: 27
I explain the trace:
10:42:59,471 INFO [STDOUT] predelete: 16; 15; 2; 27
I call a println with: the childs id (PK) - 16, the childs FK to the report - 15 the childs id to another parent table - 2, and another field containing its value - 27
Then I call manager.remove(child);
and call an println with just its value just to make sure it was deleted without exceptiosn inside.
10:42:59,471 INFO [STDOUT] postdelete: 27
Here I deleted two different ReportInstances, id=10, and id=15 respectively.
Id=10 had 3 actual childs, and in the trace it says that it has nine
Id=15 had 1 actual child, and in the trace it says that it has three
The code at the agentBean that performs the ReportInstance deletion is:
public void deleteReportInstance(ReportInstanceEJB reportInstance) { Collection <ReportParameterValueEJB> values = reportInstance.getReportParameterValues(); Iterator <ReportParameterValueEJB> valuesIt = values.iterator(); System.out.println("Number of values:" + values.size()); while (valuesIt.hasNext()){ ReportParameterValueEJB value = valuesIt.next(); System.out.println("predelete: " + value.getId()+"; "+ value.getIdReportInstance()+"; "+ value.getIdParameter()+"; "+value.getValue()); manager.remove(value); System.out.println("postdelete: " + value.getValue()); } manager.remove(reportInstance); }
I get one instance in the client, get a reference to the agentbean and ask it to delete the reportInstance as show above.
I have checked the database and I had only one child for reportInstance 10 and three for instance 15.
What's happening???
Thanks in advance