For saving the selected values in a multi-select combo what is the data structure/field recommended?
Usually it’s a many-to-many collection property.
For example:
Hobby.java
@JmixEntity
@Entity
public class Hobby {
// ...
}
User.java
@JmixEntity
@Entity
public class User {
// ...
@JoinTable(name = "USER_HOBBY_LINK",
joinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "ID"),
inverseJoinColumns = @JoinColumn(name = "HOBBY_ID", referencedColumnName = "ID"))
@ManyToMany
private List<Hobby> hobbies;
}
user-detail-view.xml
<data>
<instance id="userDc"
class="com.company.untitled2.entity.User">
<fetchPlan extends="_base">
<property name="hobbies" fetchPlan="_base"/>
</fetchPlan>
<loader/>
</instance>
<collection id="hobbiesDc" class="com.company.untitled2.entity.Hobby">
<fetchPlan extends="_base"/>
<loader id="hobbiesDl" readOnly="true">
<query>
<![CDATA[select e from Hobby e]]>
</query>
</loader>
</collection>
</data>
<layout>
<formLayout id="form" dataContainer="userDc">
<!-- ... -->
<multiSelectComboBox property="hobbies" itemsContainer="hobbiesDc"/>
</formLayout>
Thank you Konstantin.
Can this be used for one-to-may collection property too?
Yes, it will link the selected entities to the master one (User in the example above) by setting the reference to the master.
But I struggle to imagine a use case for this scenario. Do you have any idea?