Skip to content

Commit 5ee64d4

Browse files
committed
Add composite-keys entity test for upsert
1 parent c7316e2 commit 5ee64d4

File tree

8 files changed

+544
-6
lines changed

8 files changed

+544
-6
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.seasar.doma.it.criteria;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import org.seasar.doma.Entity;
6+
import org.seasar.doma.Id;
7+
import org.seasar.doma.Metamodel;
8+
import org.seasar.doma.OriginalStates;
9+
import org.seasar.doma.Transient;
10+
import org.seasar.doma.Version;
11+
12+
@Entity(metamodel = @Metamodel)
13+
public class CompKeyDepartment {
14+
15+
@Id private Integer departmentId1;
16+
@Id private Integer departmentId2;
17+
private Integer departmentNo;
18+
private String departmentName;
19+
private String location;
20+
@Version private Integer version;
21+
@OriginalStates private CompKeyDepartment originalStates;
22+
@Transient private List<Employee> employeeList = new ArrayList<>();
23+
24+
public Integer getDepartmentId1() {
25+
return departmentId1;
26+
}
27+
28+
public void setDepartmentId1(Integer departmentId1) {
29+
this.departmentId1 = departmentId1;
30+
}
31+
32+
public Integer getDepartmentId2() {
33+
return departmentId2;
34+
}
35+
36+
public void setDepartmentId2(Integer departmentId2) {
37+
this.departmentId2 = departmentId2;
38+
}
39+
40+
public Integer getDepartmentNo() {
41+
return departmentNo;
42+
}
43+
44+
public void setDepartmentNo(Integer departmentNo) {
45+
this.departmentNo = departmentNo;
46+
}
47+
48+
public String getDepartmentName() {
49+
return departmentName;
50+
}
51+
52+
public void setDepartmentName(String departmentName) {
53+
this.departmentName = departmentName;
54+
}
55+
56+
public String getLocation() {
57+
return location;
58+
}
59+
60+
public void setLocation(String location) {
61+
this.location = location;
62+
}
63+
64+
public Integer getVersion() {
65+
return version;
66+
}
67+
68+
public void setVersion(Integer version) {
69+
this.version = version;
70+
}
71+
72+
public CompKeyDepartment getOriginalStates() {
73+
return originalStates;
74+
}
75+
76+
public void setOriginalStates(CompKeyDepartment originalStates) {
77+
this.originalStates = originalStates;
78+
}
79+
80+
public List<Employee> getEmployeeList() {
81+
return employeeList;
82+
}
83+
84+
public void setEmployeeList(List<Employee> employeeList) {
85+
this.employeeList = employeeList;
86+
}
87+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.seasar.doma.it.dao;
2+
3+
import java.util.List;
4+
import org.seasar.doma.BatchInsert;
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Insert;
7+
import org.seasar.doma.Select;
8+
import org.seasar.doma.it.entity.CompKeyDept;
9+
import org.seasar.doma.jdbc.BatchResult;
10+
import org.seasar.doma.jdbc.Result;
11+
import org.seasar.doma.jdbc.query.DuplicateKeyType;
12+
13+
@Dao
14+
public interface CompKeyDeptDao {
15+
16+
@Select
17+
CompKeyDept selectByIds(Integer departmentId1, Integer departmentId2);
18+
19+
@Insert(duplicateKeyType = DuplicateKeyType.UPDATE)
20+
Result<CompKeyDept> insertOnDuplicateKeyUpdate(CompKeyDept entity);
21+
22+
@BatchInsert(duplicateKeyType = DuplicateKeyType.UPDATE)
23+
BatchResult<CompKeyDept> insertOnDuplicateKeyUpdate(List<CompKeyDept> entities);
24+
25+
@Insert(duplicateKeyType = DuplicateKeyType.IGNORE)
26+
Result<CompKeyDept> insertOnDuplicateKeyIgnore(CompKeyDept entity);
27+
28+
@BatchInsert(duplicateKeyType = DuplicateKeyType.IGNORE)
29+
BatchResult<CompKeyDept> insertOnDuplicateKeyIgnore(List<CompKeyDept> entities);
30+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.seasar.doma.it.entity;
2+
3+
import org.seasar.doma.Entity;
4+
import org.seasar.doma.Id;
5+
import org.seasar.doma.Table;
6+
import org.seasar.doma.Version;
7+
import org.seasar.doma.it.domain.Identity;
8+
import org.seasar.doma.it.domain.Location;
9+
10+
@Entity(immutable = true, listener = CompKeyDeptListener.class)
11+
@Table(name = "COMP_KEY_DEPARTMENT")
12+
public class CompKeyDept {
13+
14+
@Id final Identity<CompKeyDept> departmentId1;
15+
16+
@Id final Identity<CompKeyDept> departmentId2;
17+
18+
final Integer departmentNo;
19+
20+
final String departmentName;
21+
22+
final Location<CompKeyDept> location;
23+
24+
@Version final Integer version;
25+
26+
public CompKeyDept(
27+
Identity<CompKeyDept> departmentId1,
28+
Identity<CompKeyDept> departmentId2,
29+
Integer departmentNo,
30+
String departmentName,
31+
Location<CompKeyDept> location,
32+
Integer version) {
33+
super();
34+
this.departmentId1 = departmentId1;
35+
this.departmentId2 = departmentId2;
36+
this.departmentNo = departmentNo;
37+
this.departmentName = departmentName;
38+
this.location = location;
39+
this.version = version;
40+
}
41+
42+
/**
43+
* @return the departmentId1
44+
*/
45+
public Identity<CompKeyDept> getDepartmentId1() {
46+
return departmentId1;
47+
}
48+
49+
/**
50+
* @return the departmentId2
51+
*/
52+
public Identity<CompKeyDept> getDepartmentId2() {
53+
return departmentId2;
54+
}
55+
56+
/**
57+
* @return the departmentNo
58+
*/
59+
public Integer getDepartmentNo() {
60+
return departmentNo;
61+
}
62+
63+
/**
64+
* @return the departmentName
65+
*/
66+
public String getDepartmentName() {
67+
return departmentName;
68+
}
69+
70+
/**
71+
* @return the location
72+
*/
73+
public Location<CompKeyDept> getLocation() {
74+
return location;
75+
}
76+
77+
/**
78+
* @return the version
79+
*/
80+
public Integer getVersion() {
81+
return version;
82+
}
83+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.seasar.doma.it.entity;
2+
3+
import org.seasar.doma.jdbc.entity.EntityListener;
4+
import org.seasar.doma.jdbc.entity.PostDeleteContext;
5+
import org.seasar.doma.jdbc.entity.PostInsertContext;
6+
import org.seasar.doma.jdbc.entity.PostUpdateContext;
7+
import org.seasar.doma.jdbc.entity.PreDeleteContext;
8+
import org.seasar.doma.jdbc.entity.PreInsertContext;
9+
import org.seasar.doma.jdbc.entity.PreUpdateContext;
10+
import org.seasar.doma.jdbc.query.DuplicateKeyType;
11+
12+
public class CompKeyDeptListener implements EntityListener<CompKeyDept> {
13+
14+
@Override
15+
public void preDelete(CompKeyDept entity, PreDeleteContext<CompKeyDept> context) {
16+
CompKeyDept newEntity =
17+
new CompKeyDept(
18+
entity.departmentId1,
19+
entity.departmentId2,
20+
entity.departmentNo,
21+
entity.departmentName + "_preD",
22+
entity.location,
23+
entity.version);
24+
context.setNewEntity(newEntity);
25+
}
26+
27+
@Override
28+
public void preInsert(CompKeyDept entity, PreInsertContext<CompKeyDept> context) {
29+
CompKeyDept newEntity =
30+
new CompKeyDept(
31+
entity.departmentId1,
32+
entity.departmentId2,
33+
entity.departmentNo,
34+
entity.departmentName + "_preI(" + initialLetters(context.getDuplicateKeyType()) + ")",
35+
entity.location,
36+
entity.version);
37+
context.setNewEntity(newEntity);
38+
}
39+
40+
@Override
41+
public void preUpdate(CompKeyDept entity, PreUpdateContext<CompKeyDept> context) {
42+
CompKeyDept newEntity =
43+
new CompKeyDept(
44+
entity.departmentId1,
45+
entity.departmentId2,
46+
entity.departmentNo,
47+
entity.departmentName + "_preU",
48+
entity.location,
49+
entity.version);
50+
context.setNewEntity(newEntity);
51+
}
52+
53+
@Override
54+
public void postInsert(CompKeyDept entity, PostInsertContext<CompKeyDept> context) {
55+
CompKeyDept newEntity =
56+
new CompKeyDept(
57+
entity.departmentId1,
58+
entity.departmentId2,
59+
entity.departmentNo,
60+
entity.departmentName + "_postI(" + initialLetters(context.getDuplicateKeyType()) + ")",
61+
entity.location,
62+
entity.version);
63+
context.setNewEntity(newEntity);
64+
}
65+
66+
@Override
67+
public void postUpdate(CompKeyDept entity, PostUpdateContext<CompKeyDept> context) {
68+
CompKeyDept newEntity =
69+
new CompKeyDept(
70+
entity.departmentId1,
71+
entity.departmentId2,
72+
entity.departmentNo,
73+
entity.departmentName + "_postU",
74+
entity.location,
75+
entity.version);
76+
context.setNewEntity(newEntity);
77+
}
78+
79+
@Override
80+
public void postDelete(CompKeyDept entity, PostDeleteContext<CompKeyDept> context) {
81+
CompKeyDept newEntity =
82+
new CompKeyDept(
83+
entity.departmentId1,
84+
entity.departmentId2,
85+
entity.departmentNo,
86+
entity.departmentName + "_postD",
87+
entity.location,
88+
entity.version);
89+
context.setNewEntity(newEntity);
90+
}
91+
92+
private String initialLetters(DuplicateKeyType duplicateKeyType) {
93+
return duplicateKeyType.name().substring(0, 1);
94+
}
95+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select * from COMP_KEY_DEPARTMENT where DEPARTMENT_ID1 = /*departmentId1*/0 and DEPARTMENT_ID2 = /*departmentId2*/0

0 commit comments

Comments
 (0)