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

你。 提交于 2019-12-23 12:51:43

问题


I'm using Play! 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;
  // ..
}

回答1:


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:

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:

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 (i.e. you shouldn't have to declare the same in the two salon yml entries).



来源:https://stackoverflow.com/questions/9715000/manytomany-test-fixtures-yaml-in-play-framework-1-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!