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 +0000
> From: "c.whalley"<c.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

Reply via email to