Skip to content

Commit 91bfe5f

Browse files
committed
Support Self Joins
This required removing the equals/hashCode methods from SqlTable so tables with the same name don't get overridden in the alias map.
1 parent 49df3e4 commit 91bfe5f

File tree

7 files changed

+147
-78
lines changed

7 files changed

+147
-78
lines changed

src/main/java/org/mybatis/dynamic/sql/SqlTable.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,4 @@ public <T> SqlColumn<T> column(String name, JDBCType jdbcType, String typeHandle
4545
public static SqlTable of(String name) {
4646
return new SqlTable(name);
4747
}
48-
49-
@Override
50-
public int hashCode() {
51-
return Objects.hashCode(name);
52-
}
53-
54-
@Override
55-
public boolean equals(Object obj) {
56-
if (this == obj) {
57-
return true;
58-
}
59-
60-
if (!(obj instanceof SqlTable)) {
61-
return false;
62-
}
63-
64-
SqlTable other = (SqlTable) obj;
65-
return Objects.equals(name, other.name);
66-
}
6748
}

src/test/java/examples/joins/JoinMapper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,9 @@
1818
import java.util.List;
1919
import java.util.Map;
2020

21+
import org.apache.ibatis.annotations.Result;
2122
import org.apache.ibatis.annotations.ResultMap;
23+
import org.apache.ibatis.annotations.Results;
2224
import org.apache.ibatis.annotations.SelectProvider;
2325
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2426
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
@@ -30,4 +32,12 @@ public interface JoinMapper {
3032

3133
@SelectProvider(type=SqlProviderAdapter.class, method="select")
3234
List<Map<String, Object>> generalSelect(SelectStatementProvider selectStatement);
35+
36+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
37+
@Results ({
38+
@Result(column="user_id", property="userId"),
39+
@Result(column="user_name", property="userName"),
40+
@Result(column="parent_id", property="parentId")
41+
})
42+
List<User> selectUsers(SelectStatementProvider selectStatement);
3343
}

src/test/java/examples/joins/JoinMapperTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
package examples.joins;
1717

1818
import static examples.joins.ItemMasterDynamicSQLSupport.itemMaster;
19-
import static examples.joins.OrderDetailDynamicSQLSupport.orderDetail;
19+
import static examples.joins.OrderDetailDynamicSQLSupport.*;
2020
import static examples.joins.OrderLineDynamicSQLSupport.orderLine;
2121
import static examples.joins.OrderMasterDynamicSQLSupport.orderDate;
2222
import static examples.joins.OrderMasterDynamicSQLSupport.orderMaster;
23+
import static examples.joins.UserDynamicSQLSupport.*;
2324
import static org.assertj.core.api.Assertions.assertThat;
2425
import static org.mybatis.dynamic.sql.SqlBuilder.*;
2526

@@ -482,4 +483,32 @@ public void testFullJoin2() {
482483
assertThat(row.get("ITEM_ID")).isEqualTo(44);
483484
}
484485
}
486+
487+
@Test
488+
public void testSelf() {
489+
try (SqlSession session = sqlSessionFactory.openSession()) {
490+
JoinMapper mapper = session.getMapper(JoinMapper.class);
491+
492+
// get Bamm Bamm's parent - should be Barney
493+
SelectStatementProvider selectStatement = select(user1.userId, user1.userName, user1.parentId)
494+
.from(user1, "u1")
495+
.join(user2, "u2").on(user1.userId, equalTo(user2.parentId))
496+
.where(user2.userId, isEqualTo(4))
497+
.build()
498+
.render(RenderingStrategy.MYBATIS3);
499+
500+
String expectedStatment = "select u1.user_id, u1.user_name, u1.parent_id"
501+
+ " from User u1 join User u2 on u1.user_id = u2.parent_id"
502+
+ " where u2.user_id = #{parameters.p1,jdbcType=INTEGER}";
503+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment);
504+
505+
List<User> rows = mapper.selectUsers(selectStatement);
506+
507+
assertThat(rows.size()).isEqualTo(1);
508+
User row = rows.get(0);
509+
assertThat(row.getUserId()).isEqualTo(2);
510+
assertThat(row.getUserName()).isEqualTo("Barney");
511+
assertThat(row.getParentId()).isNull();
512+
}
513+
}
485514
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2016-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package examples.joins;
17+
18+
public class User {
19+
private Integer userId;
20+
private String userName;
21+
private Integer parentId;
22+
23+
public Integer getUserId() {
24+
return userId;
25+
}
26+
27+
public void setUserId(Integer userId) {
28+
this.userId = userId;
29+
}
30+
31+
public String getUserName() {
32+
return userName;
33+
}
34+
35+
public void setUserName(String userName) {
36+
this.userName = userName;
37+
}
38+
39+
public Integer getParentId() {
40+
return parentId;
41+
}
42+
43+
public void setParentId(Integer parentId) {
44+
this.parentId = parentId;
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2016-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package examples.joins;
17+
18+
import java.sql.JDBCType;
19+
20+
import org.mybatis.dynamic.sql.SqlColumn;
21+
import org.mybatis.dynamic.sql.SqlTable;
22+
23+
public class UserDynamicSQLSupport {
24+
public static final User1 user1 = new User1();
25+
public static final User2 user2 = new User2();
26+
27+
28+
public static final class User1 extends SqlTable {
29+
public final SqlColumn<Integer> userId = column("user_id", JDBCType.INTEGER);
30+
public final SqlColumn<String> userName = column("user_name", JDBCType.VARCHAR);
31+
public final SqlColumn<Integer> parentId = column("parent_id", JDBCType.INTEGER);
32+
33+
public User1() {
34+
super("User");
35+
}
36+
}
37+
38+
public static final class User2 extends SqlTable {
39+
public final SqlColumn<Integer> userId = column("user_id", JDBCType.INTEGER);
40+
public final SqlColumn<String> userName = column("user_name", JDBCType.VARCHAR);
41+
public final SqlColumn<Integer> parentId = column("parent_id", JDBCType.INTEGER);
42+
43+
public User2() {
44+
super("User");
45+
}
46+
}
47+
}

src/test/java/org/mybatis/dynamic/sql/SqlTableTest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/test/resources/examples/joins/CreateJoinDB.sql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2016-2017 the original author or authors.
2+
-- Copyright 2016-2018 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -18,6 +18,14 @@ drop table OrderLine if exists;
1818
drop table ItemMaster if exists;
1919
drop table OrderDetail if exists;
2020
drop table OrderMaster if exists;
21+
drop table User if exists;
22+
23+
create table User (
24+
user_id int not null,
25+
user_name varchar(30) not null,
26+
parent_id int null,
27+
primary key (user_id)
28+
);
2129

2230
create table OrderMaster (
2331
order_id int not null,
@@ -65,3 +73,7 @@ insert into OrderLine(order_id, item_id, line_number, quantity) values(2, 22, 1,
6573
insert into OrderLine(order_id, item_id, line_number, quantity) values(2, 44, 2, 1);
6674
insert into OrderLine(order_id, item_id, line_number, quantity) values(2, 66, 3, 6);
6775

76+
insert into User(user_id, user_name) values(1, 'Fred');
77+
insert into User(user_id, user_name) values(2, 'Barney');
78+
insert into User(user_id, user_name, parent_id) values(3, 'Pebbles', 1);
79+
insert into User(user_id, user_name, parent_id) values(4, 'Bamm Bamm', 2);

0 commit comments

Comments
 (0)