public class LocationBasedRole extends AbstractEntity{
@ManyToMany(fetch=FetchType.LAZY)
private Set<Role> roles=new HashSet<Role>();
@ManyToMany(fetch=FetchType.LAZY)
private Set<Location> locations=new HashSet<Location>();
}
public class Role extends AbstractEntity{
private String name;
}
public class Location extends AbstractEntity{
private String location;
}
I have an entity named locationBasedRole which has 2 properties named roles and locations. Both roles and locations have a @ManyToMany relation with locationBasedRole.
Now I want to have one property of each in a Vaadin Table.
It should be something like this,
public class UserForm extends OgsAbstractForm<User>{
MTable<LocationBasedRole> locationBasedRoleTable = new MTable<LocationBasedRole>().withHeight("100%").withWidth("100%");
@Override
protected Component createContent() {
Set<LocationBasedRole> lbRoles=new HashSet<LocationBasedRole>();
roles.addAll(locationBasedRoleFasade.findAll());
BeanItemContainer<LocationBasedRole> bean=new BeanItemContainer<LocationBasedRole>(LocationBasedRole.class);
//It returns an error on the next both lines and I know the reason, but don't know how to solve it.
// If it was no ManyToMany relation and the properties weren't a collection, it would work
bean.addNestedContainerProperty("roles.name");
bean.addNestedContainerProperty("locations.location");
bean.removeContainerProperty("persistent");
bean.removeContainerProperty("id");
bean.addAll(lbRoles);
locationBasedRoleTable.setContainerDataSource(bean);
return new VerticalLayout(locationBasedRoleTable);
}
}
When I remove the properties from the NestedContainerProperties it shows me at least something in the table.
bean.addNestedContainerProperty("roles");
bean.addNestedContainerProperty("locations");
I could use any help!
Thanks in advance!
So if I understand your question right, you want to have the Collections of your BeanItemContainer-Entity displayed in one column each?
I see two possibilities for that.
Option 1 - use a wrapper class for your
Sets and useaddNestedContainerBeanOne possibility would be to not use
Sets inside yourLocationBasedRolebut to use a wrapper class that extendsHashSet. Then you could use theaddNestedContainerBeanmethod.I created a small example with the BeanItemContainer-Entity
TeamWhich consists of a name and teamMembers. The latter is of type
Members:Which is a simple wrapper for the
Setthat contains instances ofTeamMember:As you can see in the
Membersclass, there is a methodgetMemberswhich returns a String, containing a comma separated list of the team members names.If we now use
addNestedContainerBean("teamMembers")Vaadin tries to display all properties contained in the classMembers. Vaadin will think getMembers is a getter for a String property called members and so generate a column for it.Vaadin will also display a column "empty" because it will find the
isEmptymethod ofSetand thinkemptyis a property to display in a column. So we tell Vaadin to remove that column.The final code of my example looks like:
The result looks like:
Option 2 - create fake getters and use
addNestedContainerPropertyThe only thing you have to do for this is extend your BeanItemContainer-Entity (
LocationBasedRole) and create a fake getter for eachSetyou want to be displayed in a column. In your example those two fake getters could bepublic String getTheRoles()andpublic String getTheLocations(). Then you can usebean.addNestedContainerProperty("theRoles")andbean.addNestedContainerProperty("theLocations").In my example my
TeamMemberclass (the counterpart to yourRole/Locationclasses) would still look like in the option above:And my
Teamclass (yourLocationBasedRole) would look like:Now you can tell vaadin to add the (not existing) property "members" and Vaadin will find the getter
getMembersand use this for generating the column. We also have to tell vaadin not to display the original "teamMembers" property. So the final code is:and the result looks like: