Select Component with data from a database

Hi,

I would like to manage projects and people. Each project has a responsible (a person).

In my form for adding / editing projects, I would like to choose the person who will be responsible, by displaying the list of people (registered in the database) in a select component. Then, the user selects one person, who will be set as responsible for the project.

Do you know how could I do that ?

Thanks for your help !

Implement the
Container interface
and pass this container to the constructor of the select you’re using. Depending on what you need, you might want to extend
AbstractContainer
for convenience.

There are quite a few containers that use a database and lazy loading
in the Directory
.

If you need conversions between what they use as item identifiers and the underlying item where you want to store the field value, you can use the FieldWrapper from the
CustomField add-on
.

Thanks for your answers.

The FieldWrapper is exactly what I need : I want to display the name of the responsible and to save in my database its ID.
I have a table that displays the list of projects, and when I click on a project I have the form with all the information about the selected project.
Moreover, I use HbnContainer.

But, when I select or when I add a project, I get this exception :

java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.gestionise.StoredCommand.execute(StoredCommand.java:57)
	at com.gestionise.LotView$1.itemClick(LotView.java:248)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:490)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:162)
	at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1166)
	at com.vaadin.ui.Table.handleClickEvent(Table.java:2183)
	at com.vaadin.ui.Table.changeVariables(Table.java:1998)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1288)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1214)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:730)
	at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:483)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Unknown Source)
Caused by: com.vaadin.data.Property$ConversionException: java.lang.NoSuchMethodException: java.lang.Long.getIdUtilisateur()
	at org.vaadin.addon.customfield.beanfield.BeanFieldPropertyConverter.callGetter(BeanFieldPropertyConverter.java:114)
	at org.vaadin.addon.customfield.beanfield.BeanFieldPropertyConverter.getIdForBean(BeanFieldPropertyConverter.java:98)
	at org.vaadin.addon.customfield.beanfield.BeanFieldPropertyConverter.format(BeanFieldPropertyConverter.java:49)
	at org.vaadin.addon.customfield.PropertyConverter.toString(PropertyConverter.java:168)
	at org.vaadin.addon.customfield.PropertyConverter.setPropertyDataSource(PropertyConverter.java:132)
	at org.vaadin.addon.customfield.FieldWrapper.setPropertyDataSource(FieldWrapper.java:625)
	at com.vaadin.ui.Form.setItemDataSource(Form.java:769)
	at com.gestionise.LotForm.setItemDataSource(LotForm.java:116)
	at com.gestionise.LotForm.itemSelected(LotForm.java:165)
	at com.gestionise.LotView.tableChangeUpdate(LotView.java:291)
	... 33 more
Caused by: java.lang.NoSuchMethodException: java.lang.Long.getIdUtilisateur()
	at java.lang.Class.getMethod(Unknown Source)
	at org.vaadin.addon.customfield.beanfield.BeanFieldPropertyConverter.callGetter(BeanFieldPropertyConverter.java:108)
	... 42 more

(When I select a project the form is displayed until the select with the users).

getIdUtilisateur() is a method in the User class to get its ID (type Long).

I create my select Field :


             //ComboBox select = new ComboBox("utilisateur",getUtilisateursDAO().getContainer());
            Select select = new Select("utilisateur");
            select.setContainerDataSource(getUtilisateursDAO().getContainer());
			select.setItemCaptionPropertyId("name");
            Field field = new BeanFieldWrapper<Utilisateur>(select,
                     Utilisateur.class, getUtilisateursDAO().getContainer(), "idUtilisateur");
             return field;

I create my User Container :

this.utilisateurDAO = new DAO<Utilisateur>(Utilisateur.class, dataBaseManager);
		utilisateurDAO.getContainer().addContainerProperty("idUtilisateur", Long.class, null);

Have you got any idea ? Tell me if you need more details !

I had the same problem but i solved it by setting the setNewItemsAllowed property of the Select object to false e.g.
selectObject.setNewItemsAllowed(false);