Hibernate setter method

Discussion in 'Java' started by sameergn@gmail.com, Jun 19, 2008.

  1. Guest

    Hi,

    We are using a Java bean, say User.java, which represents a database
    table, say user. User.java has a method setName(), which may do some
    (simple or complex) validations, which must be carried out when
    application code calls setName() method.

    The same validation code will be executed by hibernate when loading
    User object from database and calling setName() on it. The validation
    code need not be executed here as data is getting loaded from
    database.

    Is there any solution/pattern/recommendation to avoid this performance
    penalty?

    Our current solution is to name instance variable as _name and then
    providing a (private) set_name() method for hibernate while will not
    carry out any validations. Then provide one more method setName()
    which will set same _name instance variable but after doing whole lot
    of validations. This method must be used by application code and will
    be made public.

    This approach requires duplicating all getters and setters. Any
    comments? :)

    Thanks,
    Sameer
     
    , Jun 19, 2008
    #1
    1. Advertising

  2. Daniel Pitts Guest

    wrote:
    > Hi,
    >
    > We are using a Java bean, say User.java, which represents a database
    > table, say user. User.java has a method setName(), which may do some
    > (simple or complex) validations, which must be carried out when
    > application code calls setName() method.
    >
    > The same validation code will be executed by hibernate when loading
    > User object from database and calling setName() on it. The validation
    > code need not be executed here as data is getting loaded from
    > database.
    >
    > Is there any solution/pattern/recommendation to avoid this performance
    > penalty?
    >
    > Our current solution is to name instance variable as _name and then
    > providing a (private) set_name() method for hibernate while will not
    > carry out any validations. Then provide one more method setName()
    > which will set same _name instance variable but after doing whole lot
    > of validations. This method must be used by application code and will
    > be made public.
    >
    > This approach requires duplicating all getters and setters. Any
    > comments? :)
    >
    > Thanks,
    > Sameer

    You can configure Hibernate to access the field directly (assuming the
    field is the same name as the setter property). I don't know off the
    top of my head how to configure this, but the documentation is a good
    place to look...

    I know that if you're using Annotations, then annotating the fields,
    rather than the getters will do what I'm suggesting, leaving your
    getters/setters to do what you want (even leaving out getters/setters
    and using a non-anemic domain model).

    Hope this helps,
    Daniel.


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 20, 2008
    #2
    1. Advertising

  3. EricF Guest

    In article <>, wrote:
    >Hi,
    >
    >We are using a Java bean, say User.java, which represents a database
    >table, say user. User.java has a method setName(), which may do some
    >(simple or complex) validations, which must be carried out when
    >application code calls setName() method.
    >
    >The same validation code will be executed by hibernate when loading
    >User object from database and calling setName() on it. The validation
    >code need not be executed here as data is getting loaded from
    >database.
    >
    >Is there any solution/pattern/recommendation to avoid this performance
    >penalty?
    >
    >Our current solution is to name instance variable as _name and then
    >providing a (private) set_name() method for hibernate while will not
    >carry out any validations. Then provide one more method setName()
    >which will set same _name instance variable but after doing whole lot
    >of validations. This method must be used by application code and will
    >be made public.
    >
    >This approach requires duplicating all getters and setters. Any
    >comments? :)
    >
    >Thanks,
    >Sameer


    I'd create some validation methods and refactor.

    Eric
     
    EricF, Jun 20, 2008
    #3
  4. Mark Space Guest

    wrote:

    > This approach requires duplicating all getters and setters. Any
    > comments? :)


    I like Eric's suggestion -- reactor is always appropriate.

    You might think about something like:

    public User {
    private String name;
    private User() {};
    public void setName( String s ) {
    // Validate here
    name = s;
    }
    public String getName() {
    return name;
    }

    public static User newInstance( ResultSet rs ) {
    User u = new User();
    u.name = rs.getString("name");
    return u;
    }
    }

    In other words, only allow a new User to be created without validation
    if you have an object that was returned from the database. That way the
    data has (probably) been validated. Programmers who cheat and try to
    create their on result set to get around validation should be shot.
    Java is unfortunately pretty much an all or nothing affair with regard
    to access -- methods are either public, or their private/package-private
    and can't be used externally.

    There might be some sneaky way of guarantying that only some specially
    ordained object can create new User's from database data, but I can't
    think of one right now.

    BTW the object above pretty much represents one row of a table, not a
    full table. Adjust to taste.
     
    Mark Space, Jun 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    1,723
    Ilias Lazaridis
    Dec 27, 2004
  2. Replies:
    0
    Views:
    3,718
  3. jstorta
    Replies:
    1
    Views:
    11,764
  4. Eleanna Skouta
    Replies:
    2
    Views:
    8,437
    Arne Vajhøj
    Dec 9, 2008
  5. Le Wang
    Replies:
    2
    Views:
    835
    Knut Lickert
    Aug 13, 2011
Loading...

Share This Page