3 Replies Latest reply on Nov 17, 2005 5:53 AM by epbernard

    Using @PrePersist to modify a value

    elmosca2

      Hi *,

      I have a entity 'User' that has a property called 'password'. This property need to be encrypted in the database. I don't know if I am in the right path, but what I am trying to do is to put the logic to encrypt/decrypt the passoword in an EntityCallback. I've implemented two methods:

       @PrePersist
       @PreUpdate
       public void encryptPassword(User user) {
       MyEncrypter encrypter = new MyEncrypter();
      
       String userPass = user.getPassword();
      
       if (userPass != null) {
       String encPass = encrypter.encrypt(userPass);
       user.setPassword(encPass);
       }
       }
      
       @PostLoad
       public void decryptPassword(User user) {
       MyEncrypter encrypter = new MyEncrypter();
      
       String decPass = encrypter.decrypt(user.getPassword());
       user.setPassword(decPass);
       }
      


      Is this aproximation ok? On persisting I get an AssertionException:

      10:55:40,443 ERROR [AssertionFailure] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
      org.hibernate.AssertionFailure: dirty, but no dirty properties
       at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:239)
       at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:105)
       at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
       at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:877)
      ...
      


      It is possible to mofidy an attribute of the entity from the EntityCallback? Or, do you know of better practices for this?

      Thanks and regards,

      Bruno