Register alias via code

222 views
Skip to first unread message

raupach

unread,
May 31, 2011, 10:00:04 AM5/31/11
to mybatis-user
Hi,

is it possible to add an alias programmatically?

I tried the following:

TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment(env, transactionFactory,
ds);
Configuration configuration = new Configuration(environment);
configuration.addMapper(MitarbeiterMapper.class);
configuration.getTypeAliasRegistry().registerAlias("Mitarbeiter",
Mitarbeiter.class);
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(configuration);

No compilation errors. However at runtime I end up with:

java.lang.RuntimeException: Error parsing Mapper XML. Cause:
org.apache.ibatis.builder.BuilderException: Error resolving class .
Cause: org.apache.ibatis.type.TypeException: Could not resolve type
alias 'Mitarbeiter'. Cause: java.lang.ClassNotFoundException: Cannot
find class: Mitarbeiter
at
org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:
83)
...

Thanks in advance!

Tim

unread,
May 31, 2011, 10:04:01 PM5/31/11
to mybati...@googlegroups.com
Can you try switching the Mapper line and the type alias line?
I don't know if that would make a difference but not seeing your xml config I can't help you anymore than that.

Josh Kamau

unread,
Jun 1, 2011, 1:29:12 AM6/1/11
to mybati...@googlegroups.com
You could also use mybatis-guice . It will enable you add aliases programatically among other things.

Regards.
Josh.

Eduardo

unread,
Jun 1, 2011, 2:17:14 AM6/1/11
to mybatis-user
I am missing something...

SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(configuration);

will not parse any xml mapper file. So I wonder how XMLMapperBuilder
got called...

could you post a longer stacktrace?

raupach

unread,
Jun 1, 2011, 4:38:02 AM6/1/11
to mybatis-user
@Tim Switching lines did not help. Same error.
@ Josh Thanx. Someday I will give mybatis-guice a try.

There is no XML Configuration file. I set up the configuration via
code. Works perfect, except alias. Here is the complete Stacktrace and
the Mapper file.

java.lang.RuntimeException: Error parsing Mapper XML. Cause:
org.apache.ibatis.builder.BuilderException: Error resolving class .
Cause: org.apache.ibatis.type.TypeException: Could not resolve type
alias 'Mitarbeiter'. Cause: java.lang.ClassNotFoundException: Cannot
find class: Mitarbeiter
at
org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:
83)
at
org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:
62)
at
org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.loadXmlResource(MapperAnnotationBuilder.java:
115)
at
org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:
92)
at
org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:
45)
at
org.apache.ibatis.session.Configuration.addMapper(Configuration.java:
419)
at
de.foo.demo.data.DAOFactory.initializeSqlSessionFactory(DAOFactory.java:
49)
at de.foo.demo.data.DAOFactory.<init>(DAOFactory.java:36)
at de.foo.demo.data.MitarbeiterDAOTest.setUp(MitarbeiterDAOTest.java:
31)
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.junit.runners.model.FrameworkMethod
$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:
15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:
41)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:
27)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
76)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
49)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving
class . Cause: org.apache.ibatis.type.TypeException: Could not resolve
type alias 'Mitarbeiter'. Cause: java.lang.ClassNotFoundException:
Cannot find class: Mitarbeiter
at
org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:
69)
at
org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:
154)
at
org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:
142)
at
org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:
137)
at
org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:
79)
... 30 more
Caused by: org.apache.ibatis.type.TypeException: Could not resolve
type alias 'Mitarbeiter'. Cause: java.lang.ClassNotFoundException:
Cannot find class: Mitarbeiter
at
org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:
85)
at
org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:
93)
at
org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:
67)
... 34 more
Caused by: java.lang.ClassNotFoundException: Cannot find class:
Mitarbeiter
at
org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:
173)
at
org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:
72)
at org.apache.ibatis.io.Resources.classForName(Resources.java:235)
at
org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:
81)
... 36 more


<?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="de.foo.demo.data.MitarbeiterMapper">

<cache />

<resultMap id="mitarbeiterResult" type="Mitarbeiter">
<id property="id" column="ID"
typeHandler="de.foo.demo.data.UuidTypeHandler"/>
<result property="vorname" column="Vorname"/>
<result property="nachname" column="Nachname"/>
<result property="geburtsdatum" column="Geburtsdatum"
jdbcType="DATE"/>
<result property="straße" column="Straße"/>
<result property="plz" column="Plz"/>
<result property="ort" column="Ort"/>
<result property="festnetz" column="Festnetz"/>
<result property="mobil" column="Mobil"/>
<result property="erstellungsdatum" column="Erstellungsdatum"/>
<result property="änderungsdatum" column="Änderungsdatum"/>
</resultMap>

<sql id="selectMitarbeiterColumns">
ID,
Vorname,
Nachname,
Geburtsdatum,
Straße,
Plz,
Ort,
Festnetz,
Mobil,
Erstellungsdatum,
Änderungsdatum
</sql>

<select id="selectMitarbeiter" resultMap="mitarbeiterResult">
SELECT <include refid="selectMitarbeiterColumns"/>
FROM Mitarbeiter
</select>

<select id="selectMitarbeiterByUUID" resultMap="mitarbeiterResult">
SELECT <include refid="selectMitarbeiterColumns"/>
FROM Mitarbeiter
WHERE ID = #{id}
</select>

<insert id="insertMitarbeiter" useGeneratedKeys="false">
INSERT INTO Mitarbeiter (
ID,
Vorname,
Nachname,
Geburtsdatum,
Straße,
Plz,
Ort,
Festnetz,
Mobil
) VALUES (
#{id},
#{vorname},
#{nachname},
#{geburtsdatum,jdbcType=DATE},
#{straße},
#{plz},
#{ort},
#{festnetz},
#{mobil}
)
</insert>

<update id="updateMitarbeiter" parameterType="Mitarbeiter">
UPDATE Mitarbeiter SET
Vorname = #{vorname},
Nachname = #{nachname},
Geburtsdatum = #{geburtsdatum, jdbcType=DATE},
Straße = #{straße},
Plz = #{plz},
Ort = #{ort},
Änderungsdatum = GETDATE()
WHERE ID = #{id}
</update>

<delete id="deleteMitarbeiter" parameterType="Mitarbeiter">
DELETE FROM Mitarbeiter WHERE Id = #{id}
</delete>

</mapper>

Eduardo

unread,
Jun 1, 2011, 6:44:46 AM6/1/11
to mybatis-user
Ah, Ok I see.

Just swap these lines:
configuration.addMapper(MitarbeiterMapper.class);
configuration.getTypeAliasRegistry().registerAlias("Mitarbeiter",
Mitarbeiter.class);

mapper loading should be done after aliases are registered.

raupach

unread,
Jun 1, 2011, 7:34:50 AM6/1/11
to mybatis-user
Swapping the lines did not work. I still get the error message.
> ...
>
> read more »

raupach

unread,
Jun 1, 2011, 7:50:22 AM6/1/11
to mybatis-user
Sorry. Swapping the lines did work. I was looking at the wrong alias.

Thank you very much!
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages