DuplicateKeyException thrown during page creation
Asked by Jacopo Freddi:

Hello everybody.

I am developing a plugin for Confluence Server, which among other things is responsible of generating pages when a REST API is hit.

In my page generation manager I create a new page this way:

```
// the PageManager was injected with Spring.
Page myPage = pageManager.getPage(space.getKey(), "My Page");
if (null == myPage)
{
    myPage = new Page();
    myPage.setTitle("My Page");
    myPage.setSpace(space);
    myPage.setBodyAsString("<xml>");
    myPage.addPermission(ContentPermission.createGroupPermission(ContentPermission.EDIT_PERMISSION, "confluence-users"));
    space.getHomePage().addChild(myPage);
    // not sure if I have to set the parent/child relationship on both objects, to err on the safe side I do both.
    myPage.setParentPage(space.getHomePage());
    // saveContext was created with a (true, true, true) builder)
    pageManager.saveContentEntity(myPage, saveContext);
}
```

When the saveContentEntity method is hit, a DuplicateKeyException is thrown:

```
"A different object with the same identifier value was already associated with the session : [com.atlassian.confluence.spaces.Space#1376257]"
```

With Cause:

```
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.atlassian.confluence.spaces.Space#1376257]
```

I had the "My Page" page set up under the space homepage, but in order to test automatic creation I deleted it and purged it from the space bin. I looked around and could not find anything that pointed out I was doing something wrong.

What am I missing?

Thank you in advance.


Answers:
author: Stephen Sifers
body: Hello Jacopo,

Awesome work on developing a plugin for Confluence. Add-ons are always appreciated.

It looks there are few other reports similar to the error you are having. I don't want to attempt to paraphrase so I will reference them below:

Here is a Post for [Create new Pages in LongRunningTask](https://community.atlassian.com/t5/Answers-Developer-Questions/Create-new-Pages-in-LongRunningTask/qaq-p/541174)

Along with a reported issue the user was able to correct: [CONFSERVER-9226](https://jira.atlassian.com/browse/CONFSERVER-9226)

These should give you some guidance on how to correct your issue upon page creation. If you do get this resolved, please let us know what the solution was so other can avoid the same problems.

Regards,  
Stephen Sifers

comments: author: Jacopo Freddi
body: Hello Stephen,

I saw the post, but thought it was not relevant as it was specifically about creating pages in LongRunningTasks, while I'm doing it in a synchronous way.

As per the issue, I was able to create pages with other titles with the same code above, so I'm pretty sure that my Space is generated correctly and is "unique" in Hibernate's context. Thank you anyway for providing these references.

I think the main issue was that I was trying to create a page with the same name as another page that was manually created and destroyed: probably a fault of the garbage collector, or Spring / Hibernate caches, I'm not sure. However, I *did* manage to recreate the page manually.

If I had time, I would gladly explore if the issue is repeatable (creating and destroying / purging a page in a space, then trying to programmatically create another page with the same name and position) and maybe file a bug report if the issue persists and is not the intended behaviour.


Tags: atl-dev

confluence-app

confluence-server

server