#2651 - @OneToOne with @History, add fix and update tests

Effectively Ebean internally needs to keep a count on the number of times AS OF predicates are added (to joins + top level table). This change moves keeping that count for all joins to DbSqlContext.
This commit is contained in:
Rob Bygrave committed 2022-04-19 15:02:50 +12:00
1 parent 67fc7cdb48
commit 756e3e4a1c
11 files changed
+98 -36

No files matched your search

@@ -12,7 +12,7 @@ public class HistoryOneToOne extends BaseDomain {
String name;
@OneToOne(cascade = {CascadeType.REFRESH, CascadeType.REFRESH})
@OneToOne(cascade = CascadeType.REFRESH)
HistorylessOneToOne historylessOneToOne;
public HistoryOneToOne(String name) {
@@ -1,23 +1,28 @@
package org.tests.model.history;
import org.tests.model.draftable.BaseDomain;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import org.tests.model.draftable.BaseDomain;
@Entity
public class HistorylessOneToOne extends BaseDomain {
private String name;
/**
* This @OneToOne(mappedBy=...) side should really be FetchType.LAZY
*/
@OneToOne(mappedBy = "historylessOneToOne", cascade = CascadeType.ALL, orphanRemoval = true)//, fetch = FetchType.LAZY)
HistoryOneToOne historyOneToOne;
public HistorylessOneToOne() {}
public HistorylessOneToOne(final String name) {
this.name = name;
}
@OneToOne(mappedBy = "historylessOneToOne", cascade = CascadeType.ALL, orphanRemoval = true)
HistoryOneToOne historyOneToOne;
public HistoryOneToOne getHistoryOneToOne() {
return historyOneToOne;
}
@@ -1,33 +1,86 @@
package org.tests.model.history;
import static org.assertj.core.api.Assertions.assertThat;
import io.ebean.DB;
import io.ebean.annotation.Platform;
import io.ebean.test.LoggedSql;
import io.ebean.xtest.BaseTestCase;
import io.ebean.xtest.IgnorePlatform;
import java.sql.Timestamp;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestHistoryOneToOne extends BaseTestCase {
import java.sql.Timestamp;
import static org.assertj.core.api.Assertions.assertThat;
class TestHistoryOneToOne extends BaseTestCase {
private Long expectedLessId;
private Long expectedHistoryId;
@IgnorePlatform({Platform.ORACLE, Platform.COCKROACH})
@Test
public void test() throws InterruptedException {
void test() throws InterruptedException {
HistorylessOneToOne historylessOneToOne = new HistorylessOneToOne();
historylessOneToOne.setHistoryOneToOne(new HistoryOneToOne("one"));
DB.save(historylessOneToOne);
Thread.sleep(20);
LoggedSql.start();
expectedLessId = historylessOneToOne.getId();
expectedHistoryId = historylessOneToOne.getHistoryOneToOne().getId();
findViaHistory();
findViaLess();
findViaLess_fetch();
findViaHistory_fetch();
}
private void findViaLess() {
HistorylessOneToOne lessFetched = DB.find(HistorylessOneToOne.class)
.asOf(new Timestamp(System.currentTimeMillis()))
.findOne();
expected_findViaLess(lessFetched);
}
private void findViaLess_fetch() {
HistorylessOneToOne lessFetched = DB.find(HistorylessOneToOne.class)
.asOf(new Timestamp(System.currentTimeMillis()))
.fetch("historyOneToOne")
.findOne();
expected_findViaLess(lessFetched);
}
private void expected_findViaLess(HistorylessOneToOne lessFetched) {
assert lessFetched != null;
assertThat(lessFetched.getId()).isEqualTo(expectedLessId);
HistoryOneToOne history1 = lessFetched.getHistoryOneToOne();
assertThat(history1.getId()).isEqualTo(expectedHistoryId);
assertThat(history1.getHistorylessOneToOne().getId()).isEqualTo(expectedLessId);
}
private void findViaHistory_fetch() {
HistoryOneToOne oneFetched = DB.find(HistoryOneToOne.class)
.asOf(new Timestamp(System.currentTimeMillis()))
.fetch("historylessOneToOne")
.findOne();
expected_viaHistory(oneFetched);
}
private void findViaHistory() {
HistoryOneToOne oneFetched = DB.find(HistoryOneToOne.class)
.asOf(new Timestamp(System.currentTimeMillis()))
.findOne();
assertThat(oneFetched.getHistorylessOneToOne().getHistoryOneToOne().getId()).isEqualTo(historylessOneToOne.getHistoryOneToOne().getId());
expected_viaHistory(oneFetched);
}
List<String> sql = LoggedSql.stop();
private void expected_viaHistory(HistoryOneToOne oneFetched) {
assert oneFetched != null;
assertThat(oneFetched.getId()).isEqualTo(expectedHistoryId);
assertThat(oneFetched.getHistorylessOneToOne().getId()).isEqualTo(expectedLessId);
}
}