Re: [Resin-interest] Help with JPA
On 19/02/2015 13:38, c.whalley wrote: On 19/02/2015 02:36, Nam Nguyen wrote: Hi Carl, JPA prohibits changing the primary key. So you're right about having to do a copy-and-delete. I'm not too familiar with EclipseLink - what's the exact error you're seeing? Maybe others can chime in. Anyways, I think the easiest and most effective way is to just use plain-ole SQL. -- Nam Thanks. Or even a pointer to a better (active) place to ask? Already done SO. Pretty sure its a pure JPA question though. -- Carl ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest After further thought I fixed this, so here it is for completeness: userWithSameRegId = findUserFromRegId(userToPersist.getGCM_Dev_Reg_Id()); if (userWithSameRegId != null) { log.fine(s+RegID Existed as: + userWithSameRegId.getName()); // We're changing the primary key by cloning and deleting the rec. user = new User(); user.setName(userToPersist.getName()); user.setGCM_Dev_Reg_Id(userToPersist.getGCM_Dev_Reg_Id()); user.setGCM_Reg_Count(userWithSameRegId.getGCM_Reg_Count() + 1); // Now delete the original rec try { Query userToDeleteQuery = em.createQuery(select u from User u where u.name = ?1); userToDeleteQuery.setParameter(1, userWithSameRegId.getName()); User userToDelete = null; try { userToDelete = (User) userToDeleteQuery.getSingleResult(); em.remove(userToDelete); } catch (NoResultException nre) { } } catch (Exception e) { e.printStackTrace(); } ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest
Re: [Resin-interest] Help with JPA
On 19/02/2015 02:36, Nam Nguyen wrote: Hi Carl, JPA prohibits changing the primary key. So you're right about having to do a copy-and-delete. I'm not too familiar with EclipseLink - what's the exact error you're seeing? Maybe others can chime in. Anyways, I think the easiest and most effective way is to just use plain-ole SQL. -- Nam Thanks. Or even a pointer to a better (active) place to ask? Already done SO. Pretty sure its a pure JPA question though. -- Carl ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest
Re: [Resin-interest] Help with JPA
Hi Carl, JPA prohibits changing the primary key. So you're right about having to do a copy-and-delete. I'm not too familiar with EclipseLink - what's the exact error you're seeing? Maybe others can chime in. Anyways, I think the easiest and most effective way is to just use plain-ole SQL. -- Nam On 2/18/15 10:00 AM, resin-interest-requ...@caucho.com wrote: Date: Tue, 17 Feb 2015 21:04:47 + From: c.whalleyc.whal...@blueyonder.co.uk Subject: [Resin-interest] Help with JPA To: General Discussion for the Resin application server resin-interest@caucho.com Message-ID:54e3acef.7060...@blueyonder.co.uk Content-Type: text/plain; charset=utf-8; format=flowed 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
[Resin-interest] Help with JPA
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