Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

(Native)Select setValue does not work

Matti Maier
10 years ago Mar 28, 2012 12:21pm

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:

public class Occupation implements Serializable, DomainLogChange {

/**
* Generated UID.
*/
private static final long serialVersionUID = -3704238155631135615L;

/**
* Database identifier.
*/
private long id;

/**
* Title of the occupation.
*/
private String title;

/**
* Room which is occupied
*/
private Room room;

...getters and setters...
}

in a window I have the following code:

Occupation ourOccupation = null;
final List<Occupation> fetchAllObjects = HibernateUtil.fetchAllObjects("from Occupation");
ourOccupation = fetchAllObjects.get(0);
List<Room> list = HibernateUtil.fetchAllObjects("from Room");
final IndexedContainer ic = new IndexedContainer();
for(Room r : list) {
ic.addItem(r);
}

NativeSelect select = new NativeSelect(app.translate("room"), ic);
select.setNullSelectionAllowed(false);
select.setImmediate(true);
select.setNewItemsAllowed(false);
Property prop = new BeanItem<Occupation>(ourOccupation).getItemProperty("room");
select.setPropertyDataSource(prop);
System.out.println("value of select:" + select.getValue().getClass().getName() + " " + select.getValue().toString());
// ouput: value of select:rvs.entities.Room Raum A
select.select(ourOccupation.getRoom());

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

Matti Maier
10 years ago Mar 30, 2012 12:43pm

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:

if(super.equals(obj)) {
return true;
}
if(((Entity) obj).getId() == getId()) {
return true;
}
return false;

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

Last updated on Mar, 30th 2012
Martin Bucknall
9 years ago May 30, 2012 9:38pm
Lorenzo Sciuto
8 years ago Jun 21, 2013 4:12pm
Petrus Viljoen
8 years ago Jun 21, 2013 6:57pm
Lorenzo Sciuto
8 years ago Jun 25, 2013 11:13am
Henri Sara
8 years ago Jun 25, 2013 11:24am
Lorenzo Sciuto
8 years ago Jun 25, 2013 12:42pm
Lorenzo Sciuto
8 years ago Jun 25, 2013 1:03pm
Charles Anthony
8 years ago Jun 25, 2013 2:11pm
Petrus Viljoen
8 years ago Jun 25, 2013 2:40pm