JPAContainer ManyToMany binding with TwinColSelect and FieldGroup

JPAContainer ManyToMany binding with TwinColSelect and FieldGroup

Hi,
i’m trying to bind a property to a TwinColSelect using the FieldGroup. I’ve tried a lot of examples which i came across but nothing works.

This are my simplified entities:


Vehicle Entity


@Entity
public class Vehicles implements Serializable {
	@Id
	private Integer id;

	@Column(length = 255, nullable = false)
	private String name;
	@Column(nullable = true)
	private String description;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "t_vehicles_features", joinColumns = { @JoinColumn(name = "vehicles_id") }, inverseJoinColumns = { @JoinColumn(name = "features_id") })
	private Set<Features> features = new HashSet<Features>();
...


Features Entity


@Entity
public class Features implements Serializable {
	@Id
	private Integer id;

	@Column(length = 255, nullable = false)
	private String name;
	@Column(nullable = true)
	private String description;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "t_vehicles_features", joinColumns = { @JoinColumn(name = "features_id") }, inverseJoinColumns = { @JoinColumn(name = "vehicles_id") })
	private List<Vehicles> vehicles = new Vector<Vehicles>();
...


The component initialisation


mFeaturesTwinColSelect = new TwinColSelect();
mFeaturesTwinColSelect.setImmediate(true);
mFeaturesTwinColSelect.setNullSelectionAllowed(true);
mFeaturesTwinColSelect.setMultiSelect(true);
mFeaturesTwinColSelect.setLeftColumnCaption("Available");
mFeaturesTwinColSelect.setRightColumnCaption("Selected");
mFieldGroup.bind(mFeaturesTwinColSelect, "features");


Container datasource assignment


JPAContainer<Features> container = JPAContainerFactory.makeNonCachedReadOnly(Features.class, mEntityManager);
container.sort(new Object[] { "name" }, new boolean[]
 { true });

mFeaturesTwinColSelect.setContainerDataSource(container);
mFeaturesTwinColSelect.setItemCaptionPropertyId("name");
//Set the converter
mFeaturesTwinColSelect.setConverter(new MultiSelectConverter(mFeaturesTwinColSelect));

I really don’t get where the problem is. If anyone could help me out i would be really grateful.

Thanks in advance.

I have the same problem. I found something about relations bind with vaadin :

http://demo.vaadin.com/book-examples/book/#jpacontainer.fieldfactory.formonetoone

If you have solutions for this problem I will be grateful for sharing

Its the same for me. I think binding ManyToMany relations is broken at the moment. I could also find no working examples. I tried the current trunk version of the JPAContainer.

I reported a ticket with a bug I noticed in the MultiSelectConverter
here
. However the problem still remains although I can succesfully use the converter one time. The problem here is that I can only assign one converter per field. I use Lists in my Data Model so I had to implement a ListToSet converter (which is invoked in a extended DefaultConverterFactory). Somehow when the data source is changed the MultiSelectConverter is not invoked anymore. Any clues?

A field can only have one converter at a time.

To chain converters, you could create a class implementing Converter to which you can give a list of converters to execute as a chain. If you don’t want to go that generic, just write your converter so that it internally combines MultiSelectConverter (by delegation or duplication) with the conversion to your List datatype.