4 Replies Latest reply on Nov 16, 2001 4:21 AM by Andrzej Posiada³a

    Update in field being part of PK - problem

    Andrzej Posiada³a Newbie

      Hi all you sinners and mortals !
      Is there any immortal out there ? If yes, then he/she will be able to help me :
      Let's assume we have a CMP Entity bean

      public class EntityBean1 {
      public Long id;
      public String f1 ;
      public long f2 ;

      public void incrementF2() {
      f2++;
      }

      }

      ID is the primary key and fields f1 and f2
      belong to unique key constraint. In other words there's a unique key constraint defined in database table:
      alter table EntityBean1 add constraint EB_UQ
      unique ( f1, f2 )
      There's a row in the above table with values :
      id = 1;
      f1 = 'sth';
      f2 = 0;

      In my facade stateless session bean (CMT) I have a method with transaction attribute set to "Required" doing this:
      1 finding the bean with id = 1
      2 calling its incrementF2() method (it's a business method exposed in remote interface )
      3 creating new EntityBean1 with id = 2 (new id ) and f1 = 'sth' (the same) and f2 = 0 (old value) - and here I get an SQLException telling me that unique key constraint EB_UQ had been violated.
      All that is done within one transaction. Transaction attribute of incrementF2() method is set to "Required" as well.
      Obviously JBoss tries to create new Entity bean before updating the old one in DB.
      Have you ever met such an issue ? Do you know any workaround / solution ?
      I'd appreciate any help
      Andrzej