Hi,

I realise this is not the usual thing but I'm using EclipseLink under 
Resin so if anyone can help I'd really appreciate it.
Basically I want to know a better way to achive something I feel I'm 
doing via a kludge. I have an entity for which I effectivley want to 
rename a primary key. I know thats a no-no so my approach is to create 
the new key, copy the existing fields over then delete the old one. The 
problem is I can't figure out how to do it in 1 transaction, I get 
various errors along the lines of my entities not being managed etc. I 
want to to it in the same transaction to guarantee atomicity. So, the 
kludge is to clone the key, close and persist the trans, then do another 
one just for the delete. Sorry for the verbosity. You can see the delete 
code is in the "is nameChanged(...)" section. Thanks very much in advance.

Here we go:

@WebServlet(value="/register", name="register-servlet")
public class RegServlet extends HttpServlet {
     private static final Logger log = 
Logger.getLogger(RegServlet.class.getName());
     private static final long serialVersionUID = 1L;
     private Properties props;
     private String s;

     @PersistenceUnit(unitName="custom")
     private EntityManagerFactory emf;
     private EntityManager em;

     @Inject
     private UserTransaction ut;

         ...

     private void writeUser(User userToPersist) {
         User userWithSameRegId = null;
         boolean nameChanged = false;

         try {
             ut.begin();
             em = emf.createEntityManager();

             Query query = em.createQuery("select u from User u where 
u.name = ?1");
             query.setParameter(1, userToPersist.getName());

             User user = null;
             try {
                 user = (User) query.getSingleResult();
             } catch (NoResultException nrx) {

                 userWithSameRegId = 
findUserFromRegId(userToPersist.get_Dev_Reg_Id());
                 if (userWithSameRegId != null) {

                     log.fine(s+"RegID Existed as: " + 
userWithSameRegId.getName());

                     user = new User();
                     user.setName(userToPersist.getName());
                     ...

                     nameChanged = true;
                 } else {

                     log.fine(s+"New user");

                     user = userToPersist;
                     user.set_Reg_Count(1);
                 }
             }

             user.set_Reg_Date(new Date());
             em.persist(user);
             em.flush();

         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             try {
                 ut.commit();
             } catch (Exception commitEx) {
                 log.fine(commitEx.toString());
             }

             if (em != null && em.isOpen()) {
                 em.close();
             }
         }

         if (nameChanged) {
             try {
                 ut.begin();
                 em = emf.createEntityManager();

                 Query query = em.createQuery("select u from User u 
where u.name = ?1");
                 query.setParameter(1, userWithSameRegId.getName());

                 User user = null;
                 try {
                     user = (User) query.getSingleResult();
                     em.remove(user);
                 } catch (NoResultException nrx) {

                 }
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 try {
                     ut.commit();
                 } catch (Exception commitEx) {
                     log.fine(commitEx.toString());
                 }

                 if (em != null && em.isOpen()) {
                     em.close();
                 }
             }
         }
     }



_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest

Reply via email to