gpt4 book ai didi

java - Play Framework 1.2.x 中的 ManyToMany 测试装置 (Yaml)

转载 作者:搜寻专家 更新时间:2023-11-01 03:27:27 25 4
gpt4 key购买 nike

我正在使用 Play! 1.2.4 + Morhpia/MongoDB。

我的模特是沙龙和造型师,它们共享多对多关系。然而,我无法正确定义测试数据来表示这种关系。

这是我做的

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

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

有了这些数据,造型师就包含了对沙龙的引用,反之亦然。

如何实现双向引用?

谢谢,斯里


这是模型类..

@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;
// ..
}

最佳答案

双向引用是什么意思?

如果您的意思是您希望能够在代码中从您的沙龙实体访问造型师,那么您将需要这样的东西:

public class Salon extends Model {

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

...
}

您的 Stylist 实体可以如下所示:

public class Stylist extends Model {

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

...
}

然后你的 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

只要在 yml 中说 stylist1 属于 salon1 和 salon2 就足够了(也就是说,您不必在两个沙龙 yml 条目中声明相同的内容)。

关于java - Play Framework 1.2.x 中的 ManyToMany 测试装置 (Yaml),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9715000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com