@Test public void shouldIgnorePartialAutoMappingBehavior_InlineNestedResultMap() { // For nested resultMaps, PARTIAL works the same as NONE sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); User user = mapper.getUserWithPets_Inline(2); Assert.assertEquals(Integer.valueOf(2), user.getId()); Assert.assertEquals("User2", user.getName()); Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName()); Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName()); } finally { sqlSession.close(); } }
@Test public void shouldIgnorePartialAutoMappingBehavior_ExternalNestedResultMap() { // For nested resultMaps, PARTIAL works the same as NONE sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); User user = mapper.getUserWithPets_External(2); Assert.assertEquals(Integer.valueOf(2), user.getId()); Assert.assertEquals("User2", user.getName()); Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName()); Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName()); } finally { sqlSession.close(); } }
@Test public void shouldUpdateFinalField() { // set automapping to default partial sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); Article article = mapper.getArticle(); // Java Language Specification 17.5.3 Subsequent Modification of Final Fields // http://docs.oracle.com/javase/specs/jls/se5.0/html/memory.html#17.5.3 // The final field should be updated in mapping Assert.assertTrue("should update version in mapping", article.version > 0); } finally { sqlSession.close(); } }
private SqlSessionFactory getSqlSessionFactoryWithConstructor() { UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory(); Properties properties = new Properties(); properties.setProperty("driver", "org.hsqldb.jdbcDriver"); properties.setProperty("url", "jdbc:hsqldb:mem:extends_with_constructor"); properties.setProperty("username", "sa"); unpooledDataSourceFactory.setProperties(properties); Environment environment = new Environment("extends_with_constructor", new JdbcTransactionFactory(), unpooledDataSourceFactory.getDataSource()); Configuration configuration = new Configuration(); configuration.setEnvironment(environment); configuration.addMapper(StudentConstructorMapper.class); configuration.addMapper(TeacherMapper.class); configuration.getMappedStatementNames(); configuration.setAutoMappingBehavior(AutoMappingBehavior.NONE); return new DefaultSqlSessionFactory(configuration); }
private void settingsElement(Properties props) throws Exception { configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL"))); configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE"))); configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true)); configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory"))); configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false)); configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), true)); configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true)); configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true)); configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false)); configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE"))); configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null)); configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null)); configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false)); configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false)); configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION"))); configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER"))); configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString")); configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true)); configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage"))); configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false)); configuration.setLogPrefix(props.getProperty("logPrefix")); configuration.setLogImpl(resolveClass(props.getProperty("logImpl"))); configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory"))); }
protected Object getRowValue(ResultSet rs, ResultMap resultMap, CacheKey rowKey, ResultColumnCache resultColumnCache) throws SQLException { final ResultLoaderMap lazyLoader = instantiateResultLoaderMap(); Object resultObject = createResultObject(rs, resultMap, lazyLoader, null, resultColumnCache); if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) { final MetaObject metaObject = configuration.newMetaObject(resultObject); boolean foundValues = resultMap.getConstructorResultMappings().size() > 0; if (shouldApplyAutomaticMappings(resultMap, !AutoMappingBehavior.NONE.equals(configuration.getAutoMappingBehavior()))) { final List<String> unmappedColumnNames = resultColumnCache.getUnmappedColumnNames(resultMap, null); foundValues = applyAutomaticMappings(rs, unmappedColumnNames, metaObject, null, resultColumnCache) || foundValues; } final List<String> mappedColumnNames = resultColumnCache.getMappedColumnNames(resultMap, null); foundValues = applyPropertyMappings(rs, resultMap, mappedColumnNames, metaObject, lazyLoader, null) || foundValues; foundValues = (lazyLoader != null && lazyLoader.size() > 0) || foundValues; resultObject = foundValues ? resultObject : null; return resultObject; } return resultObject; }
private void settingsElement(Properties props) throws Exception { configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL"))); configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE"))); configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true)); configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory"))); configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false)); configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), false)); configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true)); configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true)); configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false)); configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE"))); configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null)); configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null)); configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false)); configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false)); configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION"))); configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER"))); configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString")); configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true)); configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage"))); configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false)); configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true)); configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false)); configuration.setLogPrefix(props.getProperty("logPrefix")); @SuppressWarnings("unchecked") Class<? extends Log> logImpl = (Class<? extends Log>) resolveClass(props.getProperty("logImpl")); configuration.setLogImpl(logImpl); configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory"))); }
private void settingsElement(XNode context) throws Exception { if (context != null) { Properties props = context.getChildrenAsProperties(); // Check that all settings are known to the configuration class MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory); for (Object key : props.keySet()) { if (!metaConfig.hasSetter(String.valueOf(key))) { throw new BuilderException("The setting " + key + " is not known. Make sure you spelled it correctly (case sensitive)."); } } configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL"))); configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true)); configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory"))); configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false)); configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), true)); configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true)); configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true)); configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false)); configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE"))); configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null)); configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null)); configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false)); configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false)); configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION"))); configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER"))); configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString")); configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true)); configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage"))); configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false)); configuration.setLogPrefix(props.getProperty("logPrefix")); configuration.setLogImpl(resolveClass(props.getProperty("logImpl"))); } }
private void settingsElement(Properties props) throws Exception { configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL"))); configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE"))); configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true)); configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory"))); configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false)); configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), true)); configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true)); configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true)); configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false)); configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE"))); configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null)); configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null)); configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false)); configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false)); configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION"))); configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER"))); configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString")); configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true)); configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage"))); configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false)); configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true)); configuration.setLogPrefix(props.getProperty("logPrefix")); @SuppressWarnings("unchecked") Class<? extends Log> logImpl = (Class<? extends Log>)resolveClass(props.getProperty("logImpl")); configuration.setLogImpl(logImpl); configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory"))); }
private boolean shouldApplyAutomaticMappings(ResultMap resultMap, boolean isNested) { if (resultMap.getAutoMapping() != null) { return resultMap.getAutoMapping(); } else { if (isNested) { return AutoMappingBehavior.FULL == configuration.getAutoMappingBehavior(); } else { return AutoMappingBehavior.NONE != configuration.getAutoMappingBehavior(); } } }
@Test public void shouldGetAUser() { sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); User user = mapper.getUser(1); Assert.assertEquals("User1", user.getName()); } finally { sqlSession.close(); } }
@Test public void shouldNotInheritAutoMappingInherited_InlineNestedResultMap() { sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); User user = mapper.getUserWithPets_Inline(2); Assert.assertEquals(Integer.valueOf(2), user.getId()); Assert.assertEquals("User2", user.getName()); Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName()); Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName()); } finally { sqlSession.close(); } }
@Test public void shouldNotInheritAutoMappingInherited_ExternalNestedResultMap() { sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); User user = mapper.getUserWithPets_External(2); Assert.assertEquals(Integer.valueOf(2), user.getId()); Assert.assertEquals("User2", user.getName()); Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName()); Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName()); } finally { sqlSession.close(); } }
@Test public void shouldRespectFullAutoMappingBehavior_InlineNestedResultMap() { sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); User user = mapper.getUserWithPets_Inline(2); Assert.assertEquals(Integer.valueOf(2), user.getId()); Assert.assertEquals("User2", user.getName()); Assert.assertEquals("Chien", user.getPets().get(0).getPetName()); Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName()); } finally { sqlSession.close(); } }
@Test public void shouldRespectFullAutoMappingBehavior_ExternalNestedResultMap() { sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); User user = mapper.getUserWithPets_External(2); Assert.assertEquals(Integer.valueOf(2), user.getId()); Assert.assertEquals("User2", user.getName()); Assert.assertEquals("Chien", user.getPets().get(0).getPetName()); Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName()); } finally { sqlSession.close(); } }
@Test public void shouldGetBooks() { // set automapping to default partial sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); // no errors throw List<Book> books = mapper.getBooks(); Assert.assertTrue("should return results,no errors throw", !books.isEmpty()); } finally { sqlSession.close(); } }
@Test public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception { String resource = "org/apache/ibatis/builder/MinimalMapperConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); XMLConfigBuilder builder = new XMLConfigBuilder(inputStream); Configuration config = builder.parse(); assertNotNull(config); assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.PARTIAL)); assertThat(config.getAutoMappingUnknownColumnBehavior(), is(AutoMappingUnknownColumnBehavior.NONE)); assertThat(config.isCacheEnabled(), is(true)); assertThat(config.getProxyFactory(), is(instanceOf(JavassistProxyFactory.class))); assertThat(config.isLazyLoadingEnabled(), is(false)); assertThat(config.isAggressiveLazyLoading(), is(true)); assertThat(config.isMultipleResultSetsEnabled(), is(true)); assertThat(config.isUseColumnLabel(), is(true)); assertThat(config.isUseGeneratedKeys(), is(false)); assertThat(config.getDefaultExecutorType(), is(ExecutorType.SIMPLE)); assertNull(config.getDefaultStatementTimeout()); assertNull(config.getDefaultFetchSize()); assertThat(config.isMapUnderscoreToCamelCase(), is(false)); assertThat(config.isSafeRowBoundsEnabled(), is(false)); assertThat(config.getLocalCacheScope(), is(LocalCacheScope.SESSION)); assertThat(config.getJdbcTypeForNull(), is(JdbcType.OTHER)); assertThat(config.getLazyLoadTriggerMethods(), is((Set<String>) new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString")))); assertThat(config.isSafeResultHandlerEnabled(), is(true)); assertThat(config.getDefaultScriptingLanuageInstance(), is(instanceOf(XMLLanguageDriver.class))); assertThat(config.isCallSettersOnNulls(), is(false)); assertNull(config.getLogPrefix()); assertNull(config.getLogImpl()); assertNull(config.getConfigurationFactory()); }