コード例 #1
0
 public void destroy(Integer id)
     throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException,
         Exception {
   EntityManager em = null;
   try {
     utx.begin();
     em = getEntityManager();
     User user;
     try {
       user = em.getReference(User.class, id);
       user.getId();
     } catch (EntityNotFoundException enfe) {
       throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
     }
     List<String> illegalOrphanMessages = null;
     Collection<Token> tokenCollectionOrphanCheck = user.getTokenCollection();
     for (Token tokenCollectionOrphanCheckToken : tokenCollectionOrphanCheck) {
       if (illegalOrphanMessages == null) {
         illegalOrphanMessages = new ArrayList<String>();
       }
       illegalOrphanMessages.add(
           "This User ("
               + user
               + ") cannot be destroyed since the Token "
               + tokenCollectionOrphanCheckToken
               + " in its tokenCollection field has a non-nullable uid field.");
     }
     if (illegalOrphanMessages != null) {
       throw new IllegalOrphanException(illegalOrphanMessages);
     }
     em.remove(user);
     utx.commit();
   } catch (Exception ex) {
     try {
       utx.rollback();
     } catch (Exception re) {
       throw new RollbackFailureException(
           "An error occurred attempting to roll back the transaction.", re);
     }
     throw ex;
   } finally {
     if (em != null) {
       em.close();
     }
   }
 }
コード例 #2
0
 public void create(User user) throws RollbackFailureException, Exception {
   if (user.getTokenCollection() == null) {
     user.setTokenCollection(new ArrayList<Token>());
   }
   EntityManager em = null;
   try {
     utx.begin();
     em = getEntityManager();
     Collection<Token> attachedTokenCollection = new ArrayList<Token>();
     for (Token tokenCollectionTokenToAttach : user.getTokenCollection()) {
       tokenCollectionTokenToAttach =
           em.getReference(
               tokenCollectionTokenToAttach.getClass(), tokenCollectionTokenToAttach.getVal());
       attachedTokenCollection.add(tokenCollectionTokenToAttach);
     }
     user.setTokenCollection(attachedTokenCollection);
     em.persist(user);
     for (Token tokenCollectionToken : user.getTokenCollection()) {
       User oldUidOfTokenCollectionToken = tokenCollectionToken.getUid();
       tokenCollectionToken.setUid(user);
       tokenCollectionToken = em.merge(tokenCollectionToken);
       if (oldUidOfTokenCollectionToken != null) {
         oldUidOfTokenCollectionToken.getTokenCollection().remove(tokenCollectionToken);
         oldUidOfTokenCollectionToken = em.merge(oldUidOfTokenCollectionToken);
       }
     }
     utx.commit();
   } catch (Exception ex) {
     try {
       utx.rollback();
     } catch (Exception re) {
       throw new RollbackFailureException(
           "An error occurred attempting to roll back the transaction.", re);
     }
     throw ex;
   } finally {
     if (em != null) {
       em.close();
     }
   }
 }
コード例 #3
0
 public void edit(User user)
     throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException,
         Exception {
   EntityManager em = null;
   try {
     utx.begin();
     em = getEntityManager();
     User persistentUser = em.find(User.class, user.getId());
     Collection<Token> tokenCollectionOld = persistentUser.getTokenCollection();
     Collection<Token> tokenCollectionNew = user.getTokenCollection();
     List<String> illegalOrphanMessages = null;
     for (Token tokenCollectionOldToken : tokenCollectionOld) {
       if (!tokenCollectionNew.contains(tokenCollectionOldToken)) {
         if (illegalOrphanMessages == null) {
           illegalOrphanMessages = new ArrayList<String>();
         }
         illegalOrphanMessages.add(
             "You must retain Token "
                 + tokenCollectionOldToken
                 + " since its uid field is not nullable.");
       }
     }
     if (illegalOrphanMessages != null) {
       throw new IllegalOrphanException(illegalOrphanMessages);
     }
     Collection<Token> attachedTokenCollectionNew = new ArrayList<Token>();
     for (Token tokenCollectionNewTokenToAttach : tokenCollectionNew) {
       tokenCollectionNewTokenToAttach =
           em.getReference(
               tokenCollectionNewTokenToAttach.getClass(),
               tokenCollectionNewTokenToAttach.getVal());
       attachedTokenCollectionNew.add(tokenCollectionNewTokenToAttach);
     }
     tokenCollectionNew = attachedTokenCollectionNew;
     user.setTokenCollection(tokenCollectionNew);
     user = em.merge(user);
     for (Token tokenCollectionNewToken : tokenCollectionNew) {
       if (!tokenCollectionOld.contains(tokenCollectionNewToken)) {
         User oldUidOfTokenCollectionNewToken = tokenCollectionNewToken.getUid();
         tokenCollectionNewToken.setUid(user);
         tokenCollectionNewToken = em.merge(tokenCollectionNewToken);
         if (oldUidOfTokenCollectionNewToken != null
             && !oldUidOfTokenCollectionNewToken.equals(user)) {
           oldUidOfTokenCollectionNewToken.getTokenCollection().remove(tokenCollectionNewToken);
           oldUidOfTokenCollectionNewToken = em.merge(oldUidOfTokenCollectionNewToken);
         }
       }
     }
     utx.commit();
   } catch (Exception ex) {
     try {
       utx.rollback();
     } catch (Exception re) {
       throw new RollbackFailureException(
           "An error occurred attempting to roll back the transaction.", re);
     }
     String msg = ex.getLocalizedMessage();
     if (msg == null || msg.length() == 0) {
       Integer id = user.getId();
       if (findUser(id) == null) {
         throw new NonexistentEntityException("The user with id " + id + " no longer exists.");
       }
     }
     throw ex;
   } finally {
     if (em != null) {
       em.close();
     }
   }
 }