Skip to content

Commit 52af14c

Browse files
committed
Fixes #39. Ensure that column is present in every mapping but in nested
result maps.
1 parent b0be473 commit 52af14c

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/main/java/org/apache/ibatis/mapping/ResultMapping.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ public ResultMapping build() {
140140
private void validate() {
141141
// Issue #697: cannot define both nestedQueryId and nestedResultMapId
142142
if (resultMapping.nestedQueryId != null && resultMapping.nestedResultMapId != null) {
143-
throw new IllegalStateException("Cannot define both nestedQueryId and nestedResultMapId in property " + resultMapping.property);
143+
throw new IllegalStateException("Cannot define both nestedQueryId and nestedResultMapId in mapping " + resultMapping.property);
144144
}
145145
// Issue #5: there should be no mappings without typehandler
146146
if (resultMapping.nestedQueryId == null && resultMapping.nestedResultMapId == null && resultMapping.typeHandler == null) {
147-
throw new IllegalStateException("No typehandler found for property " + resultMapping.property);
148-
}
149-
// Issue #4: column is mandatory on nested queries
150-
if (resultMapping.nestedQueryId != null && resultMapping.column == null && resultMapping.composites.size() == 0) {
151-
throw new IllegalStateException("Missing column attribute for nested select in property " + resultMapping.property);
147+
throw new IllegalStateException("No typehandler found for mapping " + resultMapping.property);
148+
}
149+
// Issue #4 and GH #39: column is optional only in nested resultmaps but not in the rest
150+
if (resultMapping.nestedResultMapId == null && resultMapping.column == null && resultMapping.composites.size() == 0) {
151+
throw new IllegalStateException("Missing column attribute for nested select in mapping " + resultMapping.property);
152152
}
153153
if (resultMapping.getResultSet() != null) {
154154
int numColums = 0;

src/test/java/org/apache/ibatis/submitted/resultmapwithassociationstest/Mapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<mapper namespace="org.apache.ibatis.submitted.resultmapwithassociationstest.Mapper">
2222
<resultMap id="personRM" type="org.apache.ibatis.submitted.resultmapwithassociationstest.Person" >
23-
<id column="id"/>
23+
<id property="id"/>
2424
<association property="address" column="id_address" javaType="org.apache.ibatis.submitted.resultmapwithassociationstest.Address">
2525
<id property="id" column="address_id" />
2626
</association>

src/test/java/org/apache/ibatis/submitted/resultmapwithassociationstest/ResultMapWithAssociationsTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
import java.sql.Connection;
2020
import java.util.List;
2121

22+
import org.apache.ibatis.exceptions.PersistenceException;
2223
import org.apache.ibatis.io.Resources;
2324
import org.apache.ibatis.jdbc.ScriptRunner;
2425
import org.apache.ibatis.session.SqlSession;
2526
import org.apache.ibatis.session.SqlSessionFactory;
2627
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2728
import org.junit.Assert;
28-
import org.junit.BeforeClass;
2929
import org.junit.Test;
3030

3131
public class ResultMapWithAssociationsTest {
3232

3333
private static SqlSessionFactory sqlSessionFactory;
3434

35-
@BeforeClass
36-
public static void setUp() throws Exception {
35+
@Test(expected=PersistenceException.class)
36+
public void testMissingColumn() throws Exception {
3737
// create a SqlSessionFactory
3838
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/resultmapwithassociationstest/mybatis-config.xml");
3939
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
@@ -50,7 +50,6 @@ public static void setUp() throws Exception {
5050
session.close();
5151
}
5252

53-
@Test
5453
public void shouldFindAllPersonRecordsWithAssociatedAddressRecord() {
5554
SqlSession sqlSession = sqlSessionFactory.openSession();
5655
try {

0 commit comments

Comments
 (0)