(Native)Select setValue does not work

Hello Together,

I have been stuck with this problem for many hours now and I have read various posts on this forum on the same topic.

I have an Occupation which references a Room, but in a form it doesn’t select the room when opening the occupation to edit.

Here is my code:
Occupation:

in a window I have the following code:

The fetchAllObject methods returns all the objects of the type Room or Occupation depending on the parameter (I’m using Hibernate).

What is wrong here?

Greetings,
Matti

OK. I can answer this to myself and to anyone who’s interested in this:

You have to override the equals() method in your unterlying entities. I assume the reason for this is, that if you load your entities from a database (using JPA, Hibernate, etc.) the system will create different objects for the same data. Thus the equals() method java provides is not sufficient.

My equals() method is simply something like:

This did the trick for me.

But I want to note:
In this case it’s possible that an object has the same id, but the contents vary, e.g. one object is still the unpersisted object (with attribute changes) and the other one is the old object from the database. In this case the objects are not the same, but this equals method will return true. To respect this circumstance you can implement a check for each attribute. I was too lazy to do this…

Thank goodness for you, I’d never have got that without help!

Just to add that since rows in different tables in the database could have identical primary key values (in my case), I had to compare the classes of the objects as well.

I’ve got problems with this because i don’t know why the equals has comparation always with null and my value.
For instance i’ve got a class Customer{ String name; }
I did addItem(new Customer(“fist c”);
then when i setValue(new Customer(“first c”);
the equals method has a:
null == “first c” only and i never have equals(Object obj) with obj != null.
What’s wrong with this?!
Any chance can this be a Vaadin bug?

equals should look something like this…


     /**
     Read carefully what javadocs say equals must do.... 
     **/
    @Override
    public boolean equals(final Object object) {
        // 
        if (this == object) { // return true if it is the same instance,
            return true;
        }
        // equals takes an Object, ensure we compare apples with apples
        if (!(object instanceof Customer)) {
            return false;
        }
        final Customer other = (Customer) object;

        // implies if EITHER instance's name is null we don't consider them equal 
        if ((this.name == null && other.name != null) || (this.name != null && !this.name.equals(other.name))) {
            return false;
        }

        return true;
    }

only with primitives should you compare with == and not the equals() method…

Thanks for replying Petrus!
Unfortunately the problem is still there…i probably explained in the wrong way the problem…
I’ll try again:
I’ve got the @Override equals method like this:

@Override
public boolean equals(final Object object) {
  System.out.println(object);
  [...]

}

The output of this equals called every time i use the

myNativeSelect.setValue(myCustomer);

is allways NULL!

How is it possible?

Just to make sure, check whether the actual object is null or just something that has a toString() that returns null or “null”.

Thanks Henri, i tested it but the object is really null…
I tried to print also the object i set in myNativeSelect.setValue(myCustomer)
and myCustomer is not null…
I can’t get the problem…

Ok here there is a coplete example…

public class TestselectUI extends UI {

	@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);

		NativeSelect sel = new NativeSelect();
		Customer c1 = new Customer("1", "Pippo");
		Customer c2 = new Customer("2", "Pluto");
		Customer c3 = new Customer("3", "Paperino");
		Customer c4 = new Customer("4", "Pantera");
		Customer c5 = new Customer("5", "Panda");

		sel.addItem(c1);
		sel.addItem(c2);
		sel.addItem(c3);
		sel.addItem(c4);
		sel.addItem(c5);

		Customer test = new Customer(c4.id, c4.name);
		sel.setValue(test);

		layout.addComponent(sel);
	}

	private class Customer {
		public String id;
		public String name;

		/**
		 * @param id
		 * @param name
		 */
		public Customer(String id, String name) {
			super();
			this.id = id;
			this.name = name;
		}

		@Override
		public String toString() {
			return this.name;
		}

		@Override
		public boolean equals(final Object object) {
			// return true if it is the same instance
			if (this == object) {
				return true;
			}
			// equals takes an Object, ensure we compare apples with apples
			if (!(object instanceof Customer)) {
				return false;
			}
			final Customer other = (Customer) object;

			// implies if EITHER instance's name is null we don't consider them equal
			if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
				return false;
			}

			return true;
		}
	}
}

What’s wrong with this?

If you override #equals() you almost always need to override #hashCode() - answered more fully on
this StackOverflow question

Cheers,

Charles.

Shame on me, completely forgot to mention hash(), the default container will use a HashMap to index the items and since the bean (Customer) is being used as the ItemID, its hashCode will most certainly be used.