EMF resource load - SAXParser loading map twice [message #1826275] |
Wed, 22 April 2020 10:52  |
Eclipse User |
|
|
|
Question copied over from Stack Overflow:
I'm having problems with persistence in EMF. The problem occurs when I add an element to a file that contains a map of elements. The process is:
- Load file
- Add element
- Save file
The first time I add an element, it works fine. But if I try to add another, it duplicates the list before adding the new element. All subsequent uses also duplicate the list.
EXAMPLE
I start with a file that looks like this. (The XML is simplified - as a new user, I can't embed links in posts, and it interprets my full XML as links :)
<primary resources>
<elements map key="1">
<value id ="1">
<keywords tags="foo">
</value>
</elements map>
</primary resources>
If I add a new element, I end up with a file that looks like this, which is correct:
<primary resources>
<elements map key="1">
<value id ="1">
<keywords tags="foo">
</value>
</elements map>
<elements map key="2">
<value id ="2">
<keywords tags="bar">
</value>
</elements map>
</primary resources>
However, if I then add another element, I get this, which is incorrect:
<primary resources>
<elements map key="1">
<value id ="1">
<keywords tags="foo">
</value>
</elements map>
<elements map key="2">
<value id ="2">
<keywords tags="bar">
</value>
</elements map>
<elements map key="1">
<value id ="1">
<keywords tags="foo">
</value>
</elements map>
<elements map key="2">
<value id ="2">
<keywords tags="bar">
</value>
</elements map>
<elements map key="3">
<value id ="3">
<keywords tags="foo">
</value>
</elements map>
</primary resources>
When I debug the code, the problem seems to be happening somewhere in the parser. I'm using the Java SE-11 System library, and the parser is
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl
Has anyone else had problems like this?
|
|
|
|
|
|
|
|
|
|
|
Re: EMF resource load - SAXParser loading map twice [message #1826312 is a reply to message #1826304] |
Thu, 23 April 2020 06:54   |
Eclipse User |
|
|
|
Here are the results of another debugging session, this time with a breakpoint in the StringToElementMapImpl constructor, as Ed suggested.
The file contains one element e1.
I load the file, and add element e2. At this point the model in memory looks like this:

I save the model to file, and then load the file again.
I have a breakpoint inside the StringToElementMapImpl ctor, so I can check to see how the map is looking at this stage. It should be empty, but it is not:

Interestingly, as German pointed out earlier, although the coreResources object is new, the primaryResources (and the elementsMap inside) have the same id. I don't know why that is - in our model coreResources has an EReference to primaryResources, and the Containment property is set to true.
Attachment: snip1.PNG
(Size: 68.04KB, Downloaded 391 times)
Attachment: snip2.PNG
(Size: 100.41KB, Downloaded 426 times)
|
|
|
|
|
|
|
|
|
|
|
Re: EMF resource load - SAXParser loading map twice [message #1826375 is a reply to message #1826348] |
Fri, 24 April 2020 03:25  |
Eclipse User |
|
|
|
Ah - found the problem.
I had originally thought that primaryResources was going to be loaded once and held in memory as a singleton. So to make that happen I overrode createPrimaryResources in CoreFactoryImpl.java. Here's the offending code:
/**
* <!-- begin-user-doc -->
* Overridden the default in order to create a singleton.
* <!-- end-user-doc --
*/
@Override
public PrimaryResources createPrimaryResources() {
if (primaryResources == null) {
primaryResources = new PrimaryResourcesImpl();
}
return primaryResources;
}
I'd completely forgotten that I'd done that! It wasn't until I started putting breakpoints around the PrimaryResources constructor that I found it again. I deleted my method, regenerated the code and now everything's fine.
Many thanks to everyone for taking the time to help me out with this, I really appreciate it.
|
|
|
Powered by
FUDForum. Page generated in 0.06372 seconds