How to populate ManyToMany relationships with YAML in Play Framework 2.1.x

I have the following ManyToMany (bi-directional) relation:

@Entity public class Proposal extends Model { ... @ManyToMany public List<Tag> tags; } @Entity public class Tag extends Model { ... @ManyToMany(mappedBy="tags") public List<Proposal> taggedProposals; } 

And I want to populate my database with some test data using the yaml file (for later display using a simple view). This is part of my yaml file:

 ... - &prop2 !!models.Proposal id: 2 title: Prop2 title proposer: *user2 - &prop3 !!models.Proposal id: 3 title: Prop3 title proposer: *user3 # Tags - &tag1 !!models.Tag name: Tag1 name desc: Tag1 description taggedProposals: - *prop1 - &tag2 !!models.Tag name: Tag2 name desc: Tag2 description taggedProposals: - *prop2 - *prop3 

The problem is that when I try to display the sentence tags or taggedProposals , ArrayLists are empty! I tried using square brackets and commas without success. All other data is loaded and displayed correctly. My test view

+4
source share
2 answers

If the answer sent by Leon Radley was accurate, that is no longer the case! The game is developing and, starting with version 2.1, the multi-valued initialization of list links now works ( see this link )! See User.zones for an example of how this works.

 zones: - &zone1 !!models.Zone id: 1 gtbName: "ZZ01" - &zone2 !!models.Zone ... users: - &user4 !!models.User id: 4 profile: *profile4 defaultZone: *zone3 zones: - *zone1 - *zone2 - *zone3 
+2
source

The problem you are facing is because the game uses ebean, and ebean does not automatically save many-to-many associations.

I had to solve it like this:

 private static void initialData() { @SuppressWarnings("unchecked") Map<String,List<Object>> all = (Map<String,List<Object>>) Yaml.load("initial-data.yml"); // Save all roles Ebean.save(all.get("roles")); // Insert users and for every user save its many-to-many association Ebean.save(all.get("users")); for(Object user: all.get("users")) { Ebean.saveManyToManyAssociations(user, "roles"); } } 

And yaml file:

 # Roles roles: - &adminRole !!models.Role name: admin - &projectleadRole !!models.Role name: projectlead # Users users: - &leonUser !!models.User email: leon@domain.com roles: - *adminRole - *projectleadRole firstName: Leon lastName: Radley 
+4
source

Source: https://habr.com/ru/post/1497653/