1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.annotion_many_one_add_columnprefix;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20
21 import java.io.Reader;
22 import java.util.List;
23
24 import org.apache.ibatis.BaseDataTest;
25 import org.apache.ibatis.io.Resources;
26 import org.apache.ibatis.session.SqlSession;
27 import org.apache.ibatis.session.SqlSessionFactory;
28 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31
32 class OneManyColumnPrefixTest {
33
34 private static SqlSessionFactory sqlSessionFactory;
35
36 @BeforeAll
37 static void setUp() throws Exception {
38
39 try (Reader reader = Resources
40 .getResourceAsReader("org/apache/ibatis/submitted/annotion_many_one_add_columnprefix/SqlMapConfig.xml")) {
41 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42 }
43
44
45 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46 "org/apache/ibatis/submitted/annotion_many_one_add_columnprefix/CreateDB.sql");
47 }
48
49 @Test
50 void shouldUseColumnPrefixWithMany() {
51 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52 UserDao mapper = sqlSession.getMapper(UserDao.class);
53 List<User> users = mapper.findAll();
54 assertNotNull(users);
55 assertEquals(4, users.size());
56 assertEquals(2, users.get(0).getRoles().size());
57 }
58 }
59
60 @Test
61 void shouldUseColumnPrefixInXmlWithMany() {
62 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
63 UserDao mapper = sqlSession.getMapper(UserDao.class);
64 List<User> users = mapper.findAll2();
65 assertNotNull(users);
66 assertEquals(4, users.size());
67 assertEquals(2, users.get(0).getRoles().size());
68 }
69 }
70
71 @Test
72 void shouldUseColumnPrefixWithOne() {
73 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
74 UserDao mapper = sqlSession.getMapper(UserDao.class);
75 List<User> users = mapper.findAll3();
76 assertNotNull(users);
77 assertEquals(2, users.size());
78 assertNotNull(users.get(0).getRole());
79 assertEquals("teacher", users.get(0).getRole().getName());
80 }
81 }
82
83 @Test
84 void shouldResolveNestedColumnPrefix() {
85 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
86 UserDao mapper = sqlSession.getMapper(UserDao.class);
87 User user = mapper.findUserWithFriend(4);
88 assertEquals(4, user.getId());
89 assertEquals(2, user.getRoles().size());
90 assertEquals("student", user.getRoles().get(0).getName());
91 assertEquals("Learning-commissary", user.getRoles().get(1).getName());
92 assertEquals(1, user.getFriend().getId());
93 assertEquals(2, user.getFriend().getRoles().size());
94 assertEquals("teacher", user.getFriend().getRoles().get(0).getName());
95 assertEquals("Headmaster", user.getFriend().getRoles().get(1).getName());
96 }
97 }
98 }