Announcement Announcement Module
No announcement yet.
Getting field value based on another Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting field value based on another

    I'm using hibernate and acegi.My user class has a method to get password but the value of the password is depend on another field value in the same class.The class looks like below (only the relevant code is stated below)

    public class User implements Serializable, UserDetails {

    protected String password;
    protected UserInfo userInfo;

    public String getPassword() {
    if (null !=getLoginType() && getLoginType().equals("pin"))
    return getUserInfo().getFirstName();
    return password;

    public String getLoginType() {
    return loginType;

    @ManyToOne(fetch = FetchType.EAGER)
    public UserInfo getUserInfo() {
    if (userInfo == null){
    userInfo = new UserInfo();
    return userInfo;

    My question is can I implement the getPassword() as above? Is it valid way of doing?
    Appreciate any suggestions.


  • #2
    Originally posted by chal View Post
    My question is can I implement the getPassword() as above? Is it valid way of doing?
    You can define any method however you like as long as it implements your application/domain logic properly. Are you wondering if there is a problem making a Hibernate persistent property's getter method do more than simply return the Hibernate-set value? Because doing so is fine. Hibernate will set the password value for you, but there is no reason you can't add additional logic within the getter method.

    One thing to note: Hibernate will not implement the logic defined in your getter method when issuing HQL queries. For instance, if you have a getter:

    public String getPassword() {
    return this.password + "_extra";

    Then the following HQL query will not find the user with the database password "1234":

    "select user " +
    "from User user " +
    "where user.password = "1234_extra"

    Sometimes people assume that the getter they define for a Hibernate persistent property carries through to the HQL queries they write; this is not the case.

    But strictly from an application point-of-view, there is nothing wrong with adding you own logic in the getter. I do so in lots of my Hibernate-based domain objects.

    Hope this helps!


    • #3
      Getting field value based on another

      Hi Arthur,
      Thanks a lot for your reply.That was what I really need to clarify.