X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=security%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fusermgt%2Fjpa%2FJpaGroupSet.java;h=a9d579e2b89a76561a319058cb9f168e780124bd;hb=74a6cd5b71b4f668ddac54c3237511ee11e8d84e;hp=1a9be6f217d8008ad20b4b339c716f4bff952545;hpb=eca69f28db6b2df1caa6cc987ab616a15d18321e;p=utils diff --git a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java index 1a9be6f2..a9d579e2 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java @@ -1,3 +1,18 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.wamblee.usermgt.jpa; import java.util.List; @@ -7,14 +22,20 @@ import java.util.TreeSet; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; +import org.wamblee.persistence.JpaMergeSupport; import org.wamblee.usermgt.Group; import org.wamblee.usermgt.GroupSet; +/** + * Group set backed by the database. + * + * @author Erik Brakkee + */ public class JpaGroupSet implements GroupSet { - - private EntityManager em; - - public JpaGroupSet(EntityManager aEm) { + + private EntityManager em; + + public JpaGroupSet(EntityManager aEm) { em = aEm; } @@ -25,17 +46,19 @@ public class JpaGroupSet implements GroupSet { return false; } em.persist(aGroup); + em.flush(); // to make sure the version is updated. return true; } @Override public boolean contains(Group aGroup) { - return find(aGroup.getName()) != null; + return find(aGroup.getName()) != null; } @Override public Group find(String aName) { - TypedQuery query = em.createNamedQuery(Group.QUERY_FIND_BY_NAME, Group.class); + TypedQuery query = em.createNamedQuery(Group.QUERY_FIND_BY_NAME, + Group.class); query.setParameter(Group.NAME_PARAM, aName); List groups = query.getResultList(); if (groups.size() > 1) { @@ -52,21 +75,29 @@ public class JpaGroupSet implements GroupSet { @Override public void groupModified(Group aGroup) { assert aGroup.getPrimaryKey() != null; - em.merge(aGroup); + Group merged = em.merge(aGroup); + // Need to flush so that version of the merged instance is updated so we + // can use + // the updated version in the original group passed in. That allows the + // same + // group object to continue to be used as a detached object. + em.flush(); + JpaMergeSupport.merge(merged, aGroup); } @Override public Set list() { - List groups = em.createNamedQuery(Group.QUERY_ALL_GROUPS, Group.class).getResultList(); + List groups = em.createNamedQuery(Group.QUERY_ALL_GROUPS, + Group.class).getResultList(); Set res = new TreeSet(groups); - return res; + return res; } @Override public boolean remove(Group aGroup) { Group group = find(aGroup.getName()); - if ( group == null ) { - return false; + if (group == null) { + return false; } em.remove(group); return true; @@ -74,7 +105,8 @@ public class JpaGroupSet implements GroupSet { @Override public int size() { - Long res = (Long)em.createNamedQuery(Group.QUERY_COUNT_GROUPS).getSingleResult(); + Long res = (Long) em.createNamedQuery(Group.QUERY_COUNT_GROUPS) + .getSingleResult(); return res.intValue(); } }