HbnContainer, ComboBox default select

Hi,

I’m using HbnContainer for ComboBox by setContainerDataSource(new HbnContainer(Person.class, new SessionManagerImpl()));

And also I use this ComboBox in Form, and form will call ComboBox.setPropertyValue, to provide one Person property for comboBox,

when the Person property is not null and the bean is not located in HbnContainer but HbnContainer has another instance of the same Person, ComboBox should selected by default, but actually it did not, no value was shown as selected in the UI for comboBox.

And suggestion or solution how could ComboBox display the selected Person?

Thank you!

You should implement correct Person.equals() method (ComboBox uses equals to find matching value)

Thank you for your quick reply,
I do implement the equals and hashCode for the Person based on the primary key id,
but I prefer not to do that, in the scenario
there is one BeanItemContainer as container for ComboBox, and user add a new Person (with id=0 by default), and comboBox will call items.addItem which will first call containId to check existing list, if the equals method is implemented, the first Person can be added into the container successfully but not for the rest, because containsId will return true to break the function.

Get back, even there is equals implemented, it doesn’t work, and my question is not about ComboBox, it is about HbnContainer, by debugging, HbnContainer only store a list of Identifiers (itemIds) for the beans, and I’m also try to loop through the list of Identifiers and by comparing to find the same Identifier with Person property, and call ComboBox.select to select that ItemId, this way not working neither.

Please help.

Could you post some code samples of your problem?
Or at least plese describe what exactly you are trying to do with hbncontainer.

Please note that hbncontainer is not intended to show not persisted entities.
It has additional methods for manipulating entities (addItem, saveEntity, removeEntity)
But after calling these methods database state changes (new entity persisted, entity updated, entity removed) and
internal container caches are flushed to catch these changes

Here is some code which supposed to be working but not:

One thing to mention, the HbnContainer’s session (get from SessionManagerImpl by creating new Session from SessionFactory) is not the same one which get the value for Person property (Session created by OpenSessionInView Filter);


public Address implements Serializable {
	privaete Long id;
	private Integer version;
	private Person person;
	...
}

public Person implements Serializable {
	private Long id;
	private Integer version;
	private String name;
	
	...

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (!id.equals(person.id)) return false;

        return true;
    }
}

public PersonComboBox extends ComboBox {
	@Override
	public PersonComboBox(String caption) {
		super(caption);
		
		setContainerDataSource(new HbnContainer(Person.class, new SessionManagerImpl());
		setItemCaptionPropertyId("name");
		setFilteringMode(FILTERINGMODE_CONTAINS);
		setImmediate(false);
		setWriteThrough(false);
	}
}

public PersonForm extends Form {
	private BeanItem<Address> item;
	
	private static String FIELD_PERSON = "person";
	private static String ....

	public PersonForm(BeanItem<Address> item) {
		setFormFieldFactory(new FormFieldFactory());
		setItemDataSource(item);
		setVisibleColumns(new String[]{person, address...});
		...
	}
	
	private class FormFieldFactory extends DefaultFieldFactory {
		private PersonComboBox personField;
		...
		
		private FormFieldFactory() {
			personField = new PersonComboBox("Person");
			...
		}
		
		@Override
		private Field createField(Item item, Object propertyId, Component uiContext) {
			Field f = super.createField(item, propertyId, uiContext);
			
			Property p = item.getItemProperty(propertyId);
			if (propertyId.equals(FIELD_PERSON)) {
				personField.setProperty(p);

                  /* Used to select default value, but not work
				 * Person person = (Person) p.getValue();
				 * for (Object itemId : personField.getContainerDataSource() ) {
				 *	// from debugging itemId is the person.id  (Long)
				 *	 if (itemId.equals(person.getId())) {
				 *		 personField.select(itemId);
				 *		 break;
				 *	 }
				 * }
                         */
				return personField;
			}
			...
		}
	
	}
}

It will be really appreciate if you can apply some working code to this common problem, as this is quite basic support for ComboBox and other Select components to use HbnContainer.

I’ve reproduced your problem.
The reson is that combobox tries to use pojo as itemid, when hibernatecontainer uses entity primary id key as itemid.

You can find more info
here

(jpacontainer and hbncontainer both use entity id as itemid, so topic is relevant for hbncontainer too)

For me it’s too hacky way, so i think its beter to use beanItemContainer for comboboxes (for small lists).

For huge lists in comboboxes (If provided workaround will not be applicatable for you) you can try to use LazyQueryContainer - it also supports lazy loading (you can find it in addons directory)

Thank you very much!

I also tried with pojo and it will throw exception that it needs Long (id) not Person class,
I will try LazyQueryContainer.
Thank you again.