0

I have 2 mapped entities:

enter image description here

@Table(name = "t_users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;
@NotNull
@Size(min = 2, max = 100)
private String name;
....
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
Set<Action> actionList = new HashSet<Action>(0);
//getters, setters}

@Entity
@Table(name = "t_actions")
public class Action implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;
@NotNull
@Size(min = 2, max = 1000)
private String action_description;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "action_initiator", nullable = false)
private User user;
//getters, setters}

And I have a form wich creates a new action, where I have to select an User. Question is: how can I select User in form? How to implement an User selection?

Code of my form:

......
Action bean = new Action();
final BeanFieldGroup<Action> binder = new BeanFieldGroup<Action>(Action.class);
binder.setItemDataSource(bean);
binder.setBuffered(true);    
fields.addComponent(binder.buildAndBind(((MyUI) "action.description", "action_description", TextField.class));
//Here must be a code of user selection
.....

I can make it so as made here Vaadin ComboBox with values and IDs

// Set the caption mode to read the caption directly
// from the 'name' property of the item
contactNameCombo.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
contactNameCombo.setItemCaptionPropertyId("name");

// Add all organization contacts to the drop-down
for (Contact contact : organizationContacts) {
contactName = contact.getName();
contactId   = contact.getId();
_logger.debug("Adding contactName=" + contactName + " contactId=" + contactId + " to person with id=" + personId);

// Note : the itemId of the item is the contactId
Item item = contactNameCombo.addItem(contactId);
item.getProperty("name").setValue(contactName)
}
// Add the contact of this person, and select it in the drop-down
contactName = person.getContact().getName();
contactId   = person.getContact().getId();
Item item = contactNameCombo.addItem(contactId);
item.getProperty("name").setValue(contactName)

// Using the itemId (which = contactId) to select the given contact
contactNameCombo.setValue(contactId);

But here i select an integer value and i need describe new field in my Class, but I need to select an "entity" , when it possible.

Question: how to populate vaadin dropdown list with User objects?

Community
  • 1
  • 1
zond
  • 1,473
  • 1
  • 21
  • 34
  • 1
    So... are you asking how to get a list of all users in the database? Or how to create a vaadin dropdown list? Or how to populate vaadin dropdown list with User objects? – 1615903 Feb 12 '16 at 12:24
  • @1615903 how to populate vaadin dropdown list with User objects – zond Feb 12 '16 at 12:29

0 Answers0