TypeException: The alias 'GeneratedCriteria' is already mapped

542 views
Skip to first unread message

David Asensio

unread,
Jan 15, 2013, 8:03:32 AM1/15/13
to mybati...@googlegroups.com
Hello,

I use mybatis-3.2.0-SNAPSHOT.jar (14/01/2013), mybatis-spring-1.1.1.jar, mybatis-generator-core-1.3.2.jar, ojdbc6.jar, *spring*-3.1.3

I'm getting the following exception when I deploy my web app:
2013-01-15T12:53:06.444+0100 | ERROR | [Thread: main] | ContextLoader | Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [persistence-context.xml]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [persistence-context.xml]: Invocation of init method failed; nested exception is org.apache.ibatis.type.TypeException: The alias 'GeneratedCriteria' is already mapped to the value 'pmf.domain.mbg.IndiceExample$GeneratedCriteria'.
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:420)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:618)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4343)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
    at org.apache.catalina.core.StandardService.start(StandardService.java:480)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:2313)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:287)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:425)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [persistence-context.xml]: Invocation of init method failed; nested exception is org.apache.ibatis.type.TypeException: The alias 'GeneratedCriteria' is already mapped to the value 'pmf.domain.mbg.IndiceExample$GeneratedCriteria'.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
    ... 30 more
Caused by: org.apache.ibatis.type.TypeException: The alias 'GeneratedCriteria' is already mapped to the value 'pmf.domain.mbg.IndiceExample$GeneratedCriteria'.
    at org.apache.ibatis.type.TypeAliasRegistry.registerAlias(TypeAliasRegistry.java:146)
    at org.apache.ibatis.type.TypeAliasRegistry.registerAlias(TypeAliasRegistry.java:139)
    at org.apache.ibatis.type.TypeAliasRegistry.registerAliases(TypeAliasRegistry.java:128)
    at org.apache.ibatis.type.TypeAliasRegistry.registerAliases(TypeAliasRegistry.java:118)
    at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:327)
    at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:295)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 37 more


If I use mybatis-3.1.1.jar I don't get the Exception. (I would like to use the 3.2.0 snapshot to solve some problems)
Even if I execute a test outside spring I don't get the Exception.

My classes are generated using MyBatis Generator 1.3.2 and, if you have used it, it declares GeneratedCriteria inner classes for each table yo need to map.
And it looks that the MapperScannerConfigurer doesn't notice the difference between them or doesn't take into account it's package.

Spring configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      http://www.springframework.org/schema/jee
      http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/pmfDS" resource-ref="true" />

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven />

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="pmf.domain" />
        <property name="typeHandlersPackage" value="pmf.persistence.util.typehandler" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="pmf.persistence" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

mybatys-config.xml for testing:
<?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>

    <typeHandlers>
    </typeHandlers>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="UNPOOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@****:****:****" />
                <property name="username" value="****" />
                <property name="password" value="****" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="pmf/persistence/mbg/EmailMapper.xml" />
        <mapper resource="pmf/persistence/mbg/IndiceMapper.xml" />
        <mapper resource="pmf/persistence/mbg/ItemMapper.xml" />
    </mappers>

</configuration>

Do I have to put any property in the generator xml or something similar with the new version of MyBatis?
What is the difference between alias doing it with 'plain' myBatis and MapperScannerConfigurer?

Thanks in advance.

David Asensio

unread,
Jan 15, 2013, 10:28:55 AM1/15/13
to mybati...@googlegroups.com
I answer myself:

I have get the same result (org.apache.ibatis.type.TypeException) declaring the typeAliases package in mybatys-config.xml.
So, I solved the problem by deleting the typeAliasesPackage property from the sqlSessionFactory bean.

Eduardo Macarron

unread,
Jan 15, 2013, 4:37:51 PM1/15/13
to mybati...@googlegroups.com
Hola David,

I am sorry but I did not understand the what the problem was.

The exception looks clear. The SqlSessionFactoryBean is trying to
register a bean that has already been registered:

org.apache.ibatis.type.TypeAliasRegistry.registerAliases(TypeAliasRegistry.java:118)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:327)

That exception would happen if aliases were also defined in the
mybatis-config.xml file but I do not see any. (At least in the snippet
you posted).

BTW there is no difference in defining them in spring or in
mybatis-config.xml. It is just a matter of taste.

Eduardo Macarron

unread,
Jan 15, 2013, 4:44:41 PM1/15/13
to mybati...@googlegroups.com
Ah, ok, I got it. It seems that the SqlSessionFactory is finding a
GeneratedCriteria in each class and registering once an again.

Are you sure it fails with 3.2.0 but works with 3.1.1 with the exact
same config?

2013/1/15 Eduardo Macarron <eduardo....@gmail.com>:

Eduardo Macarron

unread,
Mar 8, 2013, 4:39:10 PM3/8/13
to mybati...@googlegroups.com
Weird indeed. I do not recall any change in that part included in 3.2.

Can anyone fill an issue and provide a test for it?

http://code.google.com/p/mybatis/wiki/Test

2013/3/8 Vladimir Šobić <vso...@gmail.com>:
> Hello,
>
> I have exactly the same problem.I'm using mybatis 3.2.1-SNAPSHOT,
> mybats-spring 1.2.0 and spring framework 3.2.1.RELEASE.If I change
> dependencies of the project to mybatis 3.1.1, mybatis-spring 1.1.1 and
> spring framework 3.1.1 with the exact same config,everything works fine.
>
> What's the catch?
> --
> You received this message because you are subscribed to the Google Groups
> "mybatis-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mybatis-user...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

j.gorinsky

unread,
Mar 8, 2013, 7:38:38 PM3/8/13
to mybati...@googlegroups.com
I ran into a similar issue upgrading from 3.0.6 to 3.2.0. It turned out I had two classes with inner classes that shared the same name, like so:

public class DummyTypeAlias3 {

    public static  class Child1 {
        public static class Duplicate {
           
        }
    }
   
    public static class Child2 {
        public static class Duplicate {
           
        }
    }
}

Before 3.2.0, the Duplicate alias would refer to Child2$Duplicate. It seems that was fixed in this commit: https://github.com/mybatis/mybatis-3/commit/418c30e8d0404861451b2cd984fc6e55f2bbb287. Now, the alias collision produces an exception, which is probably better than arbitrarily referring to one duplicate or the other.

Eduardo Macarron

unread,
Mar 10, 2013, 1:05:36 AM3/10/13
to mybati...@googlegroups.com
Ok. Understood. There is an issue in github regarding that.

So in fact now it is working as it should. Of course we do need a
solution for this because anyone using the generator will get this
error when using package scanning.

What about skipping member classes?

Eduardo

unread,
Mar 10, 2013, 1:40:45 AM3/10/13
to mybati...@googlegroups.com

Vladimir Šobić

unread,
Mar 10, 2013, 4:42:08 AM3/10/13
to mybati...@googlegroups.com
Hello Eduardo,

You figured it correctly.I was using mybatis-generator generated classes in my project and getting exception because there is a collision with the inner class "GeneratedCriteria".

With fixes that you made 2 hours ago, everything works fine.  
Thank you very much.

Best regards,
Vladimir
Reply all
Reply to author
Forward
0 new messages