Include sql from different file (Java configuration)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Include sql from different file (Java configuration)

Riccardo Cossu
Hello,
I have a similar problem as the one posted here:
http://groups.google.com/group/mybatis-user/browse_thread/thread/12a01ee171cfe12e/851d26cc3e37c9a6?lnk=gst&q=include#851d26cc3e37c9a6

The only difference is that I use Java configuration, not xml; I can't
include sql fragments from an external file.
Here is an example of what I'm doing:

BaseBeanMapper.xml

<mapper
        namespace="BaseBeanMapper">

        <sql id="paginationStart">...

AnotherMapper.xml

<select id="mySelect" parameterType="MyParameter"
resultMap="anExistingResultMap">
                <include refid="BaseBeanMapper.paginationStart"/>...

and I get this error:

Could not find SQL statement to include with refid
'BaseBeanMapper.paginationStart'

I followed the advice of including the mappers in the right order:

configuration.addMapper(BaseBeanMapper.class);
configuration.addMapper(AnotherMapper.class);
....

and I am sure both files are loaded because AnotherMapper.xml works if
I move the sql fragments into the same file and BaseBeanMapper.xml
gives an error if it includes malfomed xml (and doesn't if the xml is
correct).

The only thing I can think of is that BaseBeanMapper has no methods,
but I use several mappers which have no methods and just mapping and
they work just fine.

There is nothing in the user guide about sql fragment and namespace,
so I'm not sure whether I'm doing it the right way; any idea?

Thanks,
Riccardo

Reply | Threaded
Open this post in threaded view
|

RE: Include sql from different file (Java configuration)

František Mejta
The Configuration.addMapper does not remember sql fragments from previously
added mappers.
You can try my workaround:

// --------------
final Configuration config = ...
final List<Class<?>> mappers = new LinkedList<~>();
mappers.add(MyMapper.class);
...

try {
    final Map<String, XNode> fragments = new HashMap<~>();
    for (final Class<?> mapper : mappers) {
        final String xml = mapper.getName().replace(".", "/") + ".xml";
        final String namespace = mapper.getName();
        final Reader reader = Resources.getResourceAsReader(xml);
        final XMLMapperBuilder builder = new XMLMapperBuilder(reader,
config, xml, fragments, namespace);
        builder.parse();
    }
} catch (final IOException ex) {
    throw new RuntimeException(ex);
}
// --------------

Hope this helps,
Franta Mejta

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of Riccardo
Sent: Friday, September 24, 2010 12:42 PM
To: mybatis-user
Subject: Include sql from different file (Java configuration)

Hello,
I have a similar problem as the one posted here:
http://groups.google.com/group/mybatis-user/browse_thread/thread/12a01ee171c
fe12e/851d26cc3e37c9a6?lnk=gst&q=include#851d26cc3e37c9a6

The only difference is that I use Java configuration, not xml; I can't
include sql fragments from an external file.
Here is an example of what I'm doing:

BaseBeanMapper.xml

<mapper
        namespace="BaseBeanMapper">

        <sql id="paginationStart">...

AnotherMapper.xml

<select id="mySelect" parameterType="MyParameter"
resultMap="anExistingResultMap">
                <include refid="BaseBeanMapper.paginationStart"/>...

and I get this error:

Could not find SQL statement to include with refid
'BaseBeanMapper.paginationStart'

I followed the advice of including the mappers in the right order:

configuration.addMapper(BaseBeanMapper.class);
configuration.addMapper(AnotherMapper.class);
....

and I am sure both files are loaded because AnotherMapper.xml works if I
move the sql fragments into the same file and BaseBeanMapper.xml gives an
error if it includes malfomed xml (and doesn't if the xml is correct).

The only thing I can think of is that BaseBeanMapper has no methods, but I
use several mappers which have no methods and just mapping and they work
just fine.

There is nothing in the user guide about sql fragment and namespace, so I'm
not sure whether I'm doing it the right way; any idea?

Thanks,
Riccardo