Results 1 to 3 of 3

Thread: Getting field value based on another

  1. #1
    Join Date
    Dec 2007
    Posts
    4

    Default Getting field value based on another

    Hi,
    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)

    @Entity
    @Table(name="user")
    public class User implements Serializable, UserDetails {

    protected String password;
    protected UserInfo userInfo;

    @Column(name="password",length=255)
    public String getPassword() {
    if (null !=getLoginType() && getLoginType().equals("pin"))
    return getUserInfo().getFirstName();
    return password;
    }

    @Column(name="login_type",nullable=true,length=3)
    public String getLoginType() {
    return loginType;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="user_info_id",nullable=false)
    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.

    Thanks

  2. #2
    Join Date
    Jul 2006
    Location
    Philadelphia, PA, USA
    Posts
    341

    Default

    Quote 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!
    Arthur Loder
    Software Engineer
    Fancast
    Comcast Interactive Media

  3. #3
    Join Date
    Dec 2007
    Posts
    4

    Default Getting field value based on another

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

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •