mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Follow up #3173 - Add test for BeanSet lazy loading OneToMany with hashCode/equals
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package org.tests.sets;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
public class O2MDepart {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
private final String name;
|
||||
|
||||
@OneToMany(cascade = CascadeType.PERSIST)
|
||||
private final Set<O2MEmp> employees = new LinkedHashSet<>();
|
||||
|
||||
public O2MDepart(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void addEmployee(O2MEmp employee) {
|
||||
this.employees.add(employee);
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Set<O2MEmp> employees() {
|
||||
return employees;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.tests.sets;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
public class O2MEmp {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
private final String code;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
O2MDepart department;
|
||||
|
||||
public O2MEmp(String name, String code) {
|
||||
this.name = name;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setDepartment(O2MDepart department) {
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
O2MEmp employee = (O2MEmp) o;
|
||||
return Objects.equals(code, employee.code);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(code);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.tests.sets;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.test.LoggedSql;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class TestO2MSet {
|
||||
|
||||
@Test
|
||||
void lazyLoadO2M_when_setWithHashCode_expect_selectProperties() {
|
||||
final O2MDepart department = new O2MDepart("Test");
|
||||
final O2MEmp employee = new O2MEmp("Test", "Code");
|
||||
department.addEmployee(employee);
|
||||
DB.save(department);
|
||||
|
||||
LoggedSql.start();
|
||||
DB.find(O2MDepart.class, department.getId())
|
||||
.employees()
|
||||
.forEach(e -> assertThat(e.getName()).isNotNull());
|
||||
|
||||
List<String> sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("select t0.id, t0.name from o2_mdepart t0 where t0.id = ?");
|
||||
assertThat(sql.get(1)).contains("select t0.department_id, t0.id, t0.code, t0.name, t0.department_id from o2_memp t0 where");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user