mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Add test for Map @OneToMany without orphanRemoval
Without orphanRemoval then map.clear() does not invoke any lazy loading.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package org.tests.sets;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
public class MapDepart2 {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private final String name;
|
||||
|
||||
@MapKey(name="code")
|
||||
@OneToMany(cascade = CascadeType.ALL) // NO ORPHAN REMOVAL
|
||||
private final Map<String,MapEmp2> employees = new LinkedHashMap<>();
|
||||
|
||||
public MapDepart2(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void addEmployee(MapEmp2 employee) {
|
||||
this.employees.put(employee.getCode(), employee);
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Map<String,MapEmp2> employees() {
|
||||
return employees;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.sets;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class MapEmp2 {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private final String code;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
MapDepart2 department;
|
||||
|
||||
public MapEmp2(String name, String code) {
|
||||
this.name = name;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setDepartment(MapDepart2 department) {
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.tests.sets;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.test.LoggedSql;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class TestO2MMap2 {
|
||||
|
||||
@Test
|
||||
void lazyLoadO2M_when_setWithHashCode_expect_selectProperties() {
|
||||
final MapDepart2 department = new MapDepart2("Test");
|
||||
department.addEmployee(new MapEmp2("Emp0", "Code0"));
|
||||
department.addEmployee(new MapEmp2("Emp1", "Code1"));
|
||||
DB.save(department);
|
||||
|
||||
MapDepart2 mapDepart = DB.find(MapDepart2.class, department.getId());
|
||||
Map<String, MapEmp2> employees = mapDepart.employees();
|
||||
assertThat(employees).hasSize(2);
|
||||
assertThat(employees).containsKeys("Code0", "Code1");
|
||||
assertThat(employees.get("Code1").getName()).isEqualTo("Emp1");
|
||||
|
||||
|
||||
LoggedSql.start();
|
||||
DB.find(MapDepart2.class, department.getId())
|
||||
.employees().forEach((k, v) -> {
|
||||
assertThat(k).isEqualTo(v.getCode());
|
||||
});
|
||||
|
||||
List<String> sql = LoggedSql.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("select t0.id, t0.name from map_depart2 t0 where t0.id = ?");
|
||||
assertThat(sql.get(1)).contains("select t0.department_id, t0.id, t0.code, t0.name, t0.department_id from map_emp2 t0 where (t0.department_id)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void beanMap_when_clear_thenAddSave() {
|
||||
|
||||
final MapDepart2 department = new MapDepart2("clearAndAdd");
|
||||
final MapEmp2 employee0 = new MapEmp2("Init1", "Code0");
|
||||
final MapEmp2 employee1 = new MapEmp2("Init2", "Code1");
|
||||
department.addEmployee(employee0);
|
||||
department.addEmployee(employee1);
|
||||
DB.save(department);
|
||||
|
||||
LoggedSql.start();
|
||||
|
||||
MapDepart2 dept = DB.find(MapDepart2.class, department.getId());
|
||||
|
||||
List<String> sql = LoggedSql.collect();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("select t0.id, t0.name from map_depart2 t0 where t0.id = ?");
|
||||
|
||||
Map<String, MapEmp2> employees = dept.employees();
|
||||
employees.clear(); // No orphan Removal to no lazy loading invoked by the clear()
|
||||
|
||||
sql = LoggedSql.collect();
|
||||
assertThat(sql).hasSize(0);
|
||||
|
||||
final MapEmp2 employee2 = new MapEmp2("After1", "Code3");
|
||||
dept.addEmployee(employee2);
|
||||
|
||||
DB.save(dept);
|
||||
|
||||
sql = LoggedSql.collect();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("insert into map_emp2 (code, name, department_id) values (?,?,?)");
|
||||
assertThat(sql.get(1)).contains(" -- bind");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user