I wouldn't go for such implementation.
Best practice is to never store a clear-text password, but a digest instead:
@Entity
public class Account
{
@Column
private String username;
@Column(length = 32)
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = DigestUtils.md5Hex(password);
}
}
It's an uncommon requirement, and JPA patterns will do their best to fight against you :)
But... some way may still be possible:
using Entity Listeners
:
@Entity
public class Account
{
@Column
private String username;
@Column
private String password;
@PostLoad
public void postLoad()
{
password = null;
}
}
be careful: when loaded inside a transaction, a null password may be eventually flushed on commit.
removing getter for password:
if you put annotations only on fields, you can remove getPassword()
method. Even if the field is populated on load, it's not accessible by external java code.
using a @Transient
combination:
@Entity
public class Account
{
@Column
private String username;
@Column
private String password;
@Transient
private String password2;
public String getPassword()
{
return password2;
}
public void setPassword(String password)
{
this.password = password;
this.password2 = password;
}
}