我尝试使用JPA容器自动生成表单。 对于多对一实体,它使用哈希码填充选择框,如图所示 -
是否可以显示映射实体的其他参数。
这些是我的实体
@Entity
@Table(name = "state")
public class State {
private String id;
private String name;
private Country country;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id", nullable = false)
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
@Entity
@Table(name="country")
public class Country {
private String id;
private String name;
private List<State> states;
@OneToMany(targetEntity=State.class, mappedBy="country", fetch=FetchType.EAGER)
public List<State> getStates() {
return states;
}
public void setStates(List<State> states) {
this.states = states;
}
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这是我的init方法 -
@Override
protected void init(VaadinRequest request) {
setupEntityManager();
FormLayout layout = new FormLayout();
setContent(layout);
final JPAContainer<State> states = JPAContainerFactory.make(State.class, "test1");
final Form stateForm = new Form();
stateForm.setCaption("State Editor");
stateForm.addStyleName("bordered"); // Custom style
stateForm.setWidth("420px");
stateForm.setBuffered(true);
stateForm.setEnabled(false);
final FieldFactory fieldFactory = new FieldFactory();
stateForm.setFormFieldFactory(fieldFactory);
stateForm.setItemDataSource(states.getItem("k"));
stateForm.setEnabled(true);
final Button save = new Button("Save");
stateForm.getFooter().removeAllComponents();
stateForm.getFooter().addComponent(save);
layout.addComponent(stateForm);
save.addListener(new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
try {
System.out.println("----------------commiting");
stateForm.commit();
stateForm.setEnabled(false);
} catch (Validator.InvalidValueException e) {
}
}
});