如何在Play Framework 2.1.x上使用YAML填充ManyToMany关系 [英] How to populate a ManyToMany relationship using YAML on Play Framework 2.1.x

查看:68
本文介绍了如何在Play Framework 2.1.x上使用YAML填充ManyToMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下ManyToMany(双向)关系:

I have the following ManyToMany (bidirectional) relationship:

@Entity
public class Proposal extends Model {
    ...
    @ManyToMany
    public List<Tag> tags;
}

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

我想使用yaml文件用一些测试数据填充数据库(以便稍后使用简单视图显示).这是我的yaml文件的一部分:

And I want to populate my DB with some test data using a yaml file (to display later 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

问题是,当我尝试显示投标的tags或标签的taggedProposals时,ArrayLists为空!我尝试使用方括号和逗号,成功.所有其他数据正在正确加载和显示.

The problem is that when I try to display a Proposal's tags or a Tag's taggedProposals, the ArrayLists are empty! I tried using square brackets and commas without success. All the other data is being loaded and displayed correctly.

推荐答案

如果莱昂·雷德利(Leon Radley)发布的答案是准确的,那就不再是事实了! Play不断发展,从2.1版本开始,现在可以使用list进行manyToMany参考初始化(看到此链接)!有关其工作方式的示例,请参见User.zones.

If the answer posted by Leon Radley was accurate, it is no longer the case ! Play evolved and, since the 2.1 version, manyToMany reference initialization by list now work (see this link) ! See User.zones for an example of how it 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

这篇关于如何在Play Framework 2.1.x上使用YAML填充ManyToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆