简体   繁体   中英

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

I have one entity 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:

@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? A Checkpoint cannot exist without CheckpointAnswer and vice versa.

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); 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 . When you save Checkpoint , your CheckpointAnswers should get persisted. That's exactly why we use ORMs. Here's some info on cascades .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM