Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Hibernate setter method

Reply
Thread Tools

Hibernate setter method

 
 
sameergn@gmail.com
Guest
Posts: n/a
 
      06-19-2008
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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      06-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/>
 
Reply With Quote
 
 
 
 
EricF
Guest
Posts: n/a
 
      06-20-2008
In article <(E-Mail Removed)>, (E-Mail Removed) 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
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-20-2008
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling setter method from initialize method Le Wang Ruby 2 08-13-2011 06:44 PM
Error in tag lib : saying "unable to find setter method for attribute" callme sanjay via JavaKB.com Java 5 04-17-2007 08:58 AM
DataBinder setter method. =?Utf-8?B?VHUtVGhhY2g=?= ASP .Net 0 03-15-2005 11:23 PM
can setter method have more than 1 parameter in JavaBeans? Matt Java 1 12-04-2004 07:10 PM
JSP taglib: Unable to find setter method for attribute kaeli Java 5 01-05-2004 07:15 PM



Advertisments