简体   繁体   English

如何创建同时引用的两个Ebean实体?

[英]How to create two Ebean entities referring to each other at the same time?

I have one entity CheckpointAnswer.java: 我有一个实体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);
}

And Checkpoint.java: 还有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();
        }
}

How can I create a Checkpoint object having some CheckpointAnswer objects? 如何创建一个Checkpoint有一些对象CheckpointAnswer对象? A Checkpoint cannot exist without CheckpointAnswer and vice versa. 一个Checkpoint不能没有存在CheckpointAnswer ,反之亦然。

I tried the approach you can see in the code but the fails. 我尝试了可以​​在代码中看到的方法,但是失败了。 I have the following unit test: 我有以下单元测试:

 @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);
}

The result: 结果:

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

Assuming assertEquals(1, checkpoints.get(0).possibleAnswers); 假设assertEquals(1, checkpoints.get(0).possibleAnswers); is failing, you probably want: 失败了,您可能想要:

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

Also unrelated, you shouldn't have to save CheckpointAnswer in your Checkpoint constructor or in addPossibleAnswers . 同样无关的,您不必将CheckpointAnswer保存在Checkpoint构造函数或addPossibleAnswers When you save Checkpoint , your CheckpointAnswers should get persisted. 保存CheckpointCheckpointAnswers应该保持不变。 That's exactly why we use ORMs. 这就是我们使用ORM的原因。 Here's some info on cascades . 这是有关层叠的一些信息。

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

相关问题 如何同时持久化两个相互关联的对象? - How persist two objects related to each other at the same time? 同时存在两个实体 - Persisting two entities at the same time 如何为相互引用的嵌套实体创建 DTO? - How do I create DTOs for nested entities referencing each other? 如何从相互引用的XML标签创建Java对象? - How to create Java objects from XML tags which are referring each other? 如何使用@OneToOne关系同时保留两个实体? - How to persist two entities at the same time with @OneToOne relationship? 如何在Ebean中对相关实体进行排序? - How can I sort related entities in Ebean? 彼此连接的两个日期选择器同时调用两个侦听器 - Two date pickers connected to each other invoke two listeners at the same time 如何使两个文档根据彼此的输入实时地相互更新? - How to make two documents update each other during real time depending on each other input? 如何使2个不相关的实体(两个存储库)同时在一个项目中运行? 可能吗? - How to make 2 unrelated entities (two repository) run in one project at the same time ? Is it possible? 如何同时从 JPA 个实体为不同的数据库创建差异更新日志文件? - How to create diff changelog file from JPA entities for different databases at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM