gpt4 book ai didi

java - 如何创建两个同时互相引用的Ebean实体?

转载 作者:行者123 更新时间:2023-12-01 13:42:06 25 4
gpt4 key购买 nike

我有一个实体 CheckpointAnswer.java:

@Entity
public class CheckpointAnswer extends Model {

@Id
public Long id;

@Column(length=160,nullable=false)
public String answer;

@ManyToOne
public Checkpoint checkpoint;

public CheckpointAnswer(String answer, Checkpoint checkpoint) {
this.answer = answer;
this.checkpoint = checkpoint;
}

public static Model.Finder<Long, CheckpointAnswer> find =
new Finder<Long, CheckpointAnswer>(Long.class, CheckpointAnswer.class);
}

和 Checkpoint.java:

@Entity
public class Checkpoint extends Model {

@Id
public Long id;

@Column(length=80,nullable=false)
public String name;

@Column(nullable=false)
public double longitude;

@Column(nullable=false)
public double latitude;

@Column(nullable=false)
public int points;

@Column(length=160,nullable=false)
public String message;

@OneToMany
public List<CheckpointAnswer> possibleAnswers = new ArrayList<CheckpointAnswer>();

@ManyToOne
public Scenario scenario;

public Checkpoint(String name, double longitude, double latitude, int points, String message, List<String> answers, Scenario scenario) {
this.name = name;
this.longitude = longitude;
this.latitude = latitude;
this.points = points;
this.message = message;
this.scenario = scenario;
for(String answer: answers) {
CheckpointAnswer ca = new CheckpointAnswer(answer, this);
ca.save();
possibleAnswers.add(ca);
}
}

public static Model.Finder<Long, Checkpoint> find =
new Finder<Long, Checkpoint>(Long.class, Checkpoint.class);

public void addPossibleAnswer(String answer) {
CheckpointAnswer checkpointAnswer = new CheckpointAnswer(answer, this);
checkpointAnswer.save();
this.possibleAnswers.add(checkpointAnswer);
this.update();
}

public static List<Checkpoint> findAssignedTo(Long scenario) {
return find.where()
.eq("scenario.id", scenario)
.findList();
}
}

如何创建具有一些 CheckpointAnswer 对象的 Checkpoint 对象?如果没有 CheckpointAnswerCheckpoint 就无法存在,反之亦然。

我尝试了您在代码中看到的方法,但失败了。我有以下单元测试:

 @Test
public void createAndRetrieveCheckpoint() {
new User("bob@gmail.com", "Bob", "secret", "000000000", USER_PRIVILEGE.regular).save();
Scenario scenario = Scenario.create("Scenario 1", false, null, "bob@gmail.com");

List<String> answers = new ArrayList<String>();
answers.add("test answer 1");
new Checkpoint("test checkpoint", 21.456, 10.2, 10, "Test question for the user", answers, scenario).save();
List<Checkpoint> checkpoints = Checkpoint.find.all();
assertNotNull(checkpoints);
assertEquals(1, checkpoints.size());
assertNotNull(checkpoints.get(0).possibleAnswers);
assertEquals(1, checkpoints.get(0).possibleAnswers);
}

结果:

[error] Test models.ModelsTest.createAndRetrieveCheckpoint failed: expected:<1> but was:<BeanList deferred >

最佳答案

假设 assertEquals(1, checkpoints.get(0).possibleAnswers); 失败,您可能需要:

assertEquals(1, checkpoints.get(0).possibleAnswers.size());

同样不相关的是,您不必将 CheckpointAnswer 保存在 Checkpoint 构造函数或 addPossibleAnswers 中。当您保存 Checkpoint 时,您的 CheckpointAnswers 应该会被保留。这正是我们使用 ORM 的原因。以下是有关 cascades 的一些信息.

关于java - 如何创建两个同时互相引用的Ebean实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20654638/

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