Binding a list element
On one page in one of my applications there is a list of organizations, and the user may pick one, and that organization is bound to a field on a bean. Seam does a nice job of allowing one to provide a list of java beans to a list, and automatically setting an object property when the user selects one. One does not have to manually search for an object with a string from a list box.
In my case, there was an error. The problem was that seam could not find the entity in the list so that it could preselect it. And when the user did select something in the list I got an error: "value is not valid." A bit of googling threw some light on the problem: I needed to override the equals() method on my Organization class.
My first attempt had a line like this (after checking for nulls and checking that the other object was the proper type:)
if (id.equals(other.id)) {
ret = true;
}
What was baffling was that this never worked. other.id was always null. When I retrieved my list of organizations, I printed out their ids, and none were null. In fact, they mapped to a not-null primary key column in the database.
I discovered that if I changed my code to read like this:
if (id.equals(other.getId())
then it worked. It must be because of the proxy class that Hibernate generates. Still, it seems like weird behaviour.