-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow up #3173 - Add test for BeanSet lazy loading OneToMany with ha…
…shCode/equals
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |