简体   繁体   English

Play Framework 1.2.x中的ManyToMany测试治具(Yaml)

[英]ManyToMany Test Fixtures (Yaml) in Play Framework 1.2.x

I'm using Play! 我正在使用Play! 1.2.4 + Morhpia / MongoDB. 1.2.4 + Morhpia / MongoDB。

My models are Salons and Stylists which share a many to many relationship. 我的模型是沙龙和造型师,它们之间存在许多关系。 I am however unable to define the test data correctly to represent this relationship. 但是,我无法正确定义测试数据来表示这种关系。

Here's what I have done 这就是我所做的

Salon(salon1):
  name: salon1
  city: singapore
  country: singapore

Stylist(stylist1):
  firstName: stylist1
  lastName: stylist1
  title: Stylist 1
  price: $100
  salons: [salon1]

With this data, the stylist contains the reference to the salon but not vice-versa. 有了这些数据,设计师就包含了沙龙的参考,反之亦然。

How to achieve two way referencing? 如何实现双向引用?

Thanks, Sri 谢谢,斯里


Here are the model classes .. 这是模型类..

@Entity("salons")
public class Salon extends Model {
  // ...
  @Reference
  @ManyToMany
  public List<Stylist> stylists;
  // ...
}

@Entity("stylists")
public class Stylist extends Model {
  // ..
  @Reference
  @ManyToMany
  public List<Salon> salons;
  // ..
}

What do you mean by two way referencing? 两种方式引用是什么意思?

If you mean you want to be able to access Stylists from your Salon entity in code, then you will need to have something like this: 如果您想通过代码从Salon实体访问Stylists,那么您将需要具有以下内容:

public class Salon extends Model {

    @ManyToMany
    @JoinTable(name = "salon_stylist", ...)
    public List<Stylist> stylists;

    ...
}

And your Stylist entity can look like this: 您的造型师实体看起来可能像这样:

public class Stylist extends Model {

    @ManyToMany
    @JoinTable(name = "salon_stylist", ...)
    public List<Salon> salons;

    ...
}        

Then your yml can look like this: 然后,您的yml可能如下所示:

Salon(salon1):
  name: salon1
  city: singapore
  country: singapore

Salon(salon2):
  name: salon2
  city: tokyo
  country: japan

Stylist(stylist1):
  firstName: stylist1
  lastName: stylist1
  title: Stylist 1
  price: $100
  salons: 
    - salon1
    - salon2

Just saying that stylist1 belongs to salon1 and salon2 should be enough in the yml (ie you shouldn't have to declare the same in the two salon yml entries). 只是说stylist1属于salon1和salon2在yml中就足够了(即,您不必在两个沙龙yml条目中声明相同的名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM