<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Load users and groups to Human task service
</h3>
<span style="margin-bottom: 10px;">
created by <a href="https://community.jboss.org/people/gkalabin">Grigory Kalabin</a> in <i>jBPM</i> - <a href="https://community.jboss.org/message/762488#762488">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hi everybody!</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I'm using jBPM 5.3.0.Final (except jbpm-human-task which version is 5.3.0.CR1, because something is wrong with my pom.xml or maven repo) and JBoss 7.1.1.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>On application startup I'm loading users and groups data to Human Task Service. Here is the code:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>void</b></font> startHumanTaskService() <font color="navy">{</font>
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(<font color="red">"org.jbpm.task"</font>);
        TaskService taskService = <font color="navy"><b>new</b></font> TaskService(emf, SystemEventListenerFactory.getSystemEventListener());
 
 
        <font color="darkgreen">// init users and groups</font>
        TaskServiceSession taskServiceSession = taskService.createSession();
        <font color="navy"><b>for</b></font> (String username : UsersUtils.getUsers()) <font color="navy">{</font>
            taskServiceSession.addUser(<font color="navy"><b>new</b></font> User(username));
        <font color="navy">}</font>
        <font color="navy"><b>for</b></font> (String group : UsersUtils.getGroups()) <font color="navy">{</font>
            <font color="darkgreen">// guest group already registered? Really?</font>
            <font color="navy"><b>if</b></font> (!group.equals(<font color="red">"guest"</font>)) <font color="navy">{</font>
                taskServiceSession.addGroup(<font color="navy"><b>new</b></font> Group(group));
            <font color="navy">}</font>
        <font color="navy">}</font>
 
 
        MinaTaskServer taskServer = <font color="navy"><b>new</b></font> MinaTaskServer(taskService);
        <font color="darkgreen">// bind to JNDI</font>
        <font color="navy"><b>try</b></font> <font color="navy">{</font>
            <font color="navy"><b>new</b></font> InitialContext().bind(TASK_SERVER_JNDI_NAME, taskServer);
            logger.info(<font color="red">"save "</font> + taskServer + <font color="red">" to "</font> + TASK_SERVER_JNDI_NAME);
        <font color="navy">}</font> <font color="navy"><b>catch</b></font> (NamingException exception) <font color="navy">{</font>
            logger.error(<font color="red">"unable to bind task server"</font>, exception);
        <font color="navy">}</font>
 
 
        <font color="darkgreen">// start the server</font>
        Thread thread = <font color="navy"><b>new</b></font> Thread(taskServer);
        thread.start();
    <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>But If I try load "guest" group I have the following exception</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">15:47:38,944 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/my-jbpm]] (MSC service thread 1-2) Exception sending context
initialized event to listener instance of class com.example.my.jbpm.MyJbpmServletContextListener: java.lang.RuntimeException: Operation failed
        at org.jbpm.task.service.TaskServiceSession.doOperationInTransaction(TaskServiceSession.java:902) [jbpm-human-task-5.3.0.CR1.jar:5.3.0.CR1]
        at org.jbpm.task.service.TaskServiceSession.persistInTransaction(TaskServiceSession.java:868) [jbpm-human-task-5.3.0.CR1.jar:5.3.0.CR1]
        at org.jbpm.task.service.TaskServiceSession.addGroup(TaskServiceSession.java:101) [jbpm-human-task-5.3.0.CR1.jar:5.3.0.CR1]
        at com.example.my.jbpm.utils.jbpm.JbpmApiUtils.startHumanTaskService(JbpmApiUtils.java:149) [classes:]
        at com.example.my.jbpm.utils.jbpm.JbpmApiUtils.initEngine(JbpmApiUtils.java:244) [classes:]
        at com.example.my.jbpm.MyJbpmServletContextListener.contextInitialized(MyJbpmServletContextListener.java:19) [classes:]
        at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
        at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_31]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_31]
        at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_31]
Caused by: javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session:
  [org.jbpm.task.Group#guest]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1359) [hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315) [hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1321) [hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:843) [hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.jbpm.task.service.persistence.TaskPersistenceManager.saveEntity(TaskPersistenceManager.java:200) [jbpm-human-task-5.3.0.CR1.jar:5.3.0.CR1]
        at org.jbpm.task.service.TaskServiceSession$14.doOperation(TaskServiceSession.java:870) [jbpm-human-task-5.3.0.CR1.jar:5.3.0.CR1]
        at org.jbpm.task.service.TaskServiceSession.doOperationInTransaction(TaskServiceSession.java:890) [jbpm-human-task-5.3.0.CR1.jar:5.3.0.CR1]
        ... 13 more
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Is that true that "guest" group already registered in jBPM? How can I check is currently inserting group persisted or not?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Thank you for any help. Have a good day!</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/762488#762488">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>