Hibernate setter method

S

sameergn

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
 
D

Daniel Pitts

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.
 
E

EricF

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
 
M

Mark Space

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top