Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how I implemented a HashMap using your models.</p> <pre><code>@Entity public class ImageModel extends Model { //All your fields, etc. @ManyToMany(mappedBy = "imagesWithTag") @MapKey(name = "name") public Map&lt;String,Tag&gt; tags = new HashMap&lt;String,Tag&gt;(); } @Entity public class Tag extends Model { // All your fields, etc. public String name; public String value; @ManyToMany public List&lt;ImageModel&gt; imagesWithTag = new ArrayList&lt;ImageModel&gt;(); } </code></pre> <p>I decided to go with Eric's suggestion of creating an entire model for Tag, as to make queries easily, but there are times where being able to use the functionalities of a Map will help a lot. This should work for EnumMaps too.</p> <pre><code>public void addTag(String tagName, String value) { Tag tag = Tag.finderTag.where().eq("name", tagName).eq("value", value).findUnique(); if(tag == null) { tag = Tag.newTag(tagName, value); } this.tags.put(tag.name, tag); this.saveManyToManyAssociations("tags"); } public static List&lt;ImageModel&gt; getImagesWithTag(String tagName, String tagValue) { Tag tag = finderTag.where().eq("name", tagName).eq("value", tagValue).findUnique(); List&lt;ImageModel&gt; images = new ArrayList&lt;ImageModel&gt;(); for(ImageModel model : tag.imagesWithTag) { images.add(model); } return images; } </code></pre> <p>I have seen barely anything about using Maps in Play Framework models, so I decided to create a small sample project (Play 2.1.1) to toy around with and further demonstrate map use. </p> <p><a href="https://github.com/Raymond26/play-framework-demos/tree/master/PlayMap" rel="nofollow">https://github.com/Raymond26/play-framework-demos/tree/master/PlayMap</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload