@ColumnTransformer isn't used in select

Viewed 529

[EDIT] Found the solution :

The problem was that the primary key is the field username. It seems that Hibernate doesn't handle this case. So I switched the primary key to an another field to fix it and it works !


I'm trying to use encryption on my database but I have some problems when I want to get the data.

The column transformer isn't used in the select clause but is working in the where clause.

This error appears when the query is a select * of the table like :

    public interface UserRepository extends JpaRepository<User, String> {

        @Query(value = "select a from User as a where username = :username")
        AdminUser findByUsername(@Param("username") String username);

    }

(The @Query is useless in this case but it's to have an example)

The generated query by Hibernate looks like :

SELECT user0_.username AS username1_0_, user0_.creation_date AS creation2_0_, user0_.last_update_date AS last3_0_, user0_.enabled AS enabled4_0_,  AES_DECRYPT(user0_.email, unhex('myKey'),'myVector') AS email5_0_, user0_.password AS password6_0_, user0_.password_expiration_date AS password7_0_, user0_.profil_id AS profil8_0_

FROM my_user_table user0_ 

WHERE AES_DECRYPT(user0_.username, unhex('myKey'), 'myVector') = 'userTest';

In case of a query like :

@Query(value = "select a.username from User as a where username = :username")

The select clause is decrypted.

Any ideas why Hibernate doesn't use the columnTransformer in the select ? Thanks,

     @Table(name = "my_user_table")
    public class User extends AbstractEntity implements UserDetails {

        @Id
        @Column(name = "username", nullable = false, unique = true, columnDefinition = "${encryption.column.definition}")
        @ColumnTransformer(
                  forColumn = "username",
                  read ="${User.username.read}", 
                  write ="${User.username.write}")  
        private String username;

        @Column(name = "password", nullable = false)
        private String password;


        @Column(name = "email", nullable = false, columnDefinition = "${encryption.column.definition}")
        @ColumnTransformer(
                  read ="${User.email.read}", 
                  write ="${User.email.write}")
        private String mailAddress;

        @Column(name = "password_expiration_date", nullable = false)
        private Date passwordExpirationDate;

        @OneToMany(targetEntity = OldPassword.class, fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, orphanRemoval = true)
        @JoinColumn(name = "username", nullable = true)
        @OrderBy("creationDate desc")
        private List<OldPassword> oldPasswords;

        /**
         * Use this field to enable or disable an account manually
         */
        @Column(name = "enabled", columnDefinition = "bit default 0", nullable = false)
        private boolean enabled;

        @Transient
        private Collection<? extends GrantedAuthority> authorities;

        @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
        @JoinColumn(name = "profil_id", referencedColumnName = "id")
        private Profil profil;

        @ElementCollection(fetch = FetchType.EAGER)
        @CollectionTable(name = "user_places", joinColumns = @JoinColumn(name = "username"))
        private List<String> userPlaces;
    }

0 Answers
Related