I have a problem on the following situation:
In my Spring, Hibernate application I got a User Entity and a UserCategory Entity. The table of the user entity got a username as identifier. This indentifierfield can’t be encrypted because this table is also used by an older program without the possibility to do this.
To make a ManyToOne reference from UserCategory to User I need a field in the UserCategory table with the unique username of a User. What I want to do is to encrypt the username in the UserCategory table using Jasypt. And of course this work:
@Type(type="encryptedString")
@ManyToOne
@JoinColumn(name = "username", insertable=false, updatable=false)
@ForeignKey(name = "none")
public User getUser(){
return this.user;
}
public void setUser(User user ){
this.user = user;
}
But after putting the username encrypted in the UserCategory table I can’t use this record because Hibernate can’t make a reference to the User on encrypted field:
You will get the following error:
"No row with the given identifier exists: com.foo.bar.models.User#M9LgndiyCsVGqfVRVblb3A=="
This is a logical error, but do you know a good solution. In think the code need something to first decrypt and then try to make the reference. But I’m stuck on how to do this.
I think you misunderstood how to use a custom user type and, as I commented in my previous answer, you are NOT supposed to declare the custom type at the association level here, you are supposed to use it at the attribute level i.e. on the
usernameattribute of theUserentity.This is actually explained in the documentation (pasted from Google Cache as the Jasypt site seems to be currently down):
So, to sum up, 1) the
@Typeannotation should be applied to theusernameand 2) you won’t be able to use theusernameas primary key (since it can’t be part of a join as mentioned in the last paragraph above).This means that you’ll need something like this (assuming you declared the appropriate
@TypeDef):And modify the
ManyToOneassociation accordingly.