Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-1968 Multiple ResultMap useAautoMappingCache issue #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gh-1968/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```sh
$ svn export https://github.com/harawata/mybatis-issues/trunk/gh-1968
$ cd gh-1968
$ mvn test
```
18 changes: 18 additions & 0 deletions gh-1968/over-v3.3.0-failed/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mybatis.issues</groupId>
<artifactId>gh-1968</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>over-v3.3.0-failed</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
</dependencies>
</project>
22 changes: 22 additions & 0 deletions gh-1968/over-v3.3.0-failed/src/test/java/test/AnotherEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test;

public class AnotherEntity {
private Integer id;
private String otherField;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getOtherField() {
return otherField;
}

public void setOtherField(String otherField) {
this.otherField = otherField;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.Reader;
import java.sql.Connection;
import java.util.List;

/**
* All code is same as module 'v3.3.0-worked'
* Except mybatis version on maven pom is '3.5.5'
* (Actually any version above 3.3.0 will make the this test fail)
*/
public class AutoMappingCacheTest {

private static SqlSessionFactory sqlSessionFactory;

@BeforeClass
public static void setUp() throws Exception {
// create an SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader("test/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
// prepare in-memory database
try (SqlSession session = sqlSessionFactory.openSession();
Connection conn = session.getConnection();
Reader reader = Resources.getResourceAsReader("test/CreateDB.sql");
Reader spReader = Resources.getResourceAsReader("test/StoreProcedure.sql")) {
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.runScript(reader);

ScriptRunner spRunner = new ScriptRunner(conn);
spRunner.setLogWriter(null);
spRunner.setSendFullScript(true);
spRunner.runScript(spReader);
}
}

@Test
@SuppressWarnings("unchecked")
public void shouldGetMultipleResult() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
List<List<?>> results = mapper.getMultipleResultSet();
List<AnotherEntity> anotherEntities = (List<AnotherEntity>) results.get(0);
List<User> userWithInner = (List<User>) results.get(1);
List<User> userWithoutInner = (List<User>) results.get(2);

Assert.assertEquals(2, anotherEntities.size());
Assert.assertEquals(4, userWithInner.size());
Assert.assertEquals(2, userWithoutInner.size());
userWithInner.forEach(user -> {
Assert.assertNotNull(user.getInnerEntity());
Assert.assertNotNull(user.getInnerEntity().getComplexCalculated());
});
userWithoutInner.forEach(user -> {
Assert.assertNull(user.getInnerEntity());
});
}
}

}
31 changes: 31 additions & 0 deletions gh-1968/over-v3.3.0-failed/src/test/java/test/InnerEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package test;

public class InnerEntity {
private Integer id;
private Integer complexCalculated;
private User user;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getComplexCalculated() {
return complexCalculated;
}

public void setComplexCalculated(Integer complexCalculated) {
this.complexCalculated = complexCalculated;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}
7 changes: 7 additions & 0 deletions gh-1968/over-v3.3.0-failed/src/test/java/test/Mapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test;

import java.util.List;

public interface Mapper {
List<List<?>> getMultipleResultSet();
}
32 changes: 32 additions & 0 deletions gh-1968/over-v3.3.0-failed/src/test/java/test/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package test;

public class User {
private Integer id;
private String name;

private InnerEntity innerEntity;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public InnerEntity getInnerEntity() {
return innerEntity;
}

public void setInnerEntity(InnerEntity innerEntity) {
this.innerEntity = innerEntity;
}
}
13 changes: 13 additions & 0 deletions gh-1968/over-v3.3.0-failed/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<Appenders>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%5p [%t] - %m%n" charset="utf-8" />
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="stdout" />
</Root>
</Loggers>
</Configuration>
36 changes: 36 additions & 0 deletions gh-1968/over-v3.3.0-failed/src/test/resources/test/CreateDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
drop table users if exists;
drop table inner_entity if exists ;
drop table another_entity if exists ;
create table users (
id int,
name varchar(20)
);

create table inner_entity
(
id int,
user_id int,
complexCalculated int
);

create table another_entity
(
id int,
otherField varchar(20)
);

insert into users (id, name) values
(1, 'PatternOne'),
(2, 'PatternOne'),
(3, 'PatternTwo'),
(4, 'PatternTwo');

insert into inner_entity (id, user_id, complexCalculated) values
(1, 1, 111),
(2, 1, 1111),
(3, 2, 222),
(4, 2, 2222);

insert into another_entity (id, otherField) values
(1, 'EntityOne'),
(2, 'EntityTwo');
21 changes: 21 additions & 0 deletions gh-1968/over-v3.3.0-failed/src/test/resources/test/Mapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="test.Mapper">

<resultMap id="anotherEntityMap" type="test.AnotherEntity">
<id column="id" />
<result property="otherField" column="otherField" />
</resultMap>
<!--We need autoMapping, but can't use association in our real use case-->
<resultMap id="multiResultSetMap" type="test.User" autoMapping="true">
<id column="id" />
<result property="name" column="name" />
</resultMap>
<select id="getMultipleResultSet" resultMap="anotherEntityMap,multiResultSetMap,multiResultSetMap">
{call get_multi_resultSets()}
</select>

</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE PROCEDURE get_multi_resultSets()
READS SQL DATA DYNAMIC RESULT SETS 3
BEGIN ATOMIC
DECLARE result1 CURSOR WITH RETURN FOR
SELECT id, otherField FROM another_entity FOR READ ONLY;

DECLARE result2 CURSOR WITH RETURN FOR
SELECT u.id,name,
ie.id as "innerEntity.id",
ie.user_id as "innerEntity.user.id",
ie.complexCalculated AS "innerEntity.complexCalculated"
FROM users u inner join inner_entity ie on u.id = ie.user_id
WHERE name='PatternOne' FOR READ ONLY;

DECLARE result3 CURSOR WITH RETURN FOR
SELECT * FROM users
WHERE name = 'PatternTwo' FOR READ ONLY;

OPEN result1; OPEN result2; OPEN result3;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:mybatisissues" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper class="test.Mapper" />
</mappers>

</configuration>
37 changes: 37 additions & 0 deletions gh-1968/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mybatis.issues</groupId>
<artifactId>gh-1968</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>v3.3.0-worked</module>
<module>over-v3.3.0-failed</module>
</modules>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
18 changes: 18 additions & 0 deletions gh-1968/v3.3.0-worked/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mybatis.issues</groupId>
<artifactId>gh-1968</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>v3.3.0-worked</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>
22 changes: 22 additions & 0 deletions gh-1968/v3.3.0-worked/src/test/java/test/AnotherEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test;

public class AnotherEntity {
private Integer id;
private String otherField;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getOtherField() {
return otherField;
}

public void setOtherField(String otherField) {
this.otherField = otherField;
}
}
Loading