我有2个映射实体:
@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}
我有一个表单创建一个新动作,我必须选择一个用户。 问题是:如何在表单中选择用户?如何实现用户选择?
我的表格代码:
......
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
.....
我可以这样做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);
但是在这里我选择一个整数值,我需要在我的类中描述新字段,但我需要在可能的情况下选择一个“实体”。
问题:如何用用户对象填充vaadin下拉列表?