问题
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