Tree structure handling by MyBatis

821 views
Skip to first unread message

demosfen

unread,
Dec 22, 2011, 4:31:48 PM12/22/11
to mybatis-user
Hello,

I've got a table that maps to itself. I tried to construct correct
mapping file but I was unsuccessful and ended up with exception
### Cause: java.sql.SQLException: ORA-00900: invalid SQL statement
; bad SQL grammar []; nested exception is java.sql.SQLException:
ORA-00900: invalid SQL statement

Is there any possibility to do such a mapping?

My files:

<?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="package_name.mappers.PeriodMapper">
<resultMap id="periodResultMap" type="package_name.Period">
<id property="id" column="id_period"/>
<result property="periodname" column="name"/>
<result property="kod" column="code"/>
<result property="begdate" column="begdate"/>
<result property="enddate" column="enddate"/>
<result property="flag" column="flag"/>
<association property="parent" column="par_id_period"
resultMap="periodResuiltMap" select="selectPeriod"/>
</resultMap>
<select id="selectPeriod" parameterType="Long"
resultMap="periodResultMap">
SELECT * FROM period WHERE id_period = #{id}
</select>
</mapper>

CREATE TABLE period
(id_period NUMBER NOT NULL,
periodname VARCHAR2(255),
kod VARCHAR2(8),
par_id_period NUMBER,
begdate DATE,
enddate DATE,
flag VARCHAR2(2))
and bean:

public class Period {
private Long id;
private String name;
private String code;
private Period parent;
private Date begDate;
private Date endDate;
public enum Flag {
Y, Q, M
}
private Flag flag;
get... set...
}

Thanks,
Andrew

Bokie

unread,
Dec 23, 2011, 6:30:32 AM12/23/11
to mybatis-user
From the exception you've posted, I don't think the problem is in your
mapping, are you sure #{id} is being correctly set.
Try to get MyBatis to log some output so you can see if it is being
correctly set. Also in your PeriodMapper.java try
selectPeriod(@Param("id") Long id)

After you have sorted that out make sure that the asterisk (*) in your
select statement is actually returning the correct column names.
IMHO it is best to stay away from * - be specific about which columns
you want.

Regards
Jorge

c c

unread,
Dec 23, 2011, 6:08:53 PM12/23/11
to mybati...@googlegroups.com
<select id="selectPeriod" parameterType="Long"
resultMap="periodResultMap">
       SELECT * FROM period WHERE id_period = #{id}
       </select>


this have some problem



2011/12/23 Bokie <jms.c...@gmail.com>

demosfen

unread,
Dec 24, 2011, 10:11:56 AM12/24/11
to mybatis-user
I fixed problem with "bad SQL grammar" exception, the problem was
incorrect annotation in PeriodMapper interface. Data loading started
in some way ... but failed with:
java.lang.OutOfMemoryError: Java heap space
java.util.Arrays.copyOfRange(Arrays.java:3209)
java.lang.String.<init>(String.java:215)
java.lang.StringBuilder.toString(StringBuilder.java:430)
java.lang.Throwable.toString(Throwable.java:344)
java.lang.String.valueOf(String.java:2826)
java.lang.StringBuilder.append(StringBuilder.java:115)

org.apache.ibatis.executor.resultset.NestedResultSetHandler.applyNestedResultMappings(NestedResultSetHandler.java:
152)

org.apache.ibatis.executor.resultset.NestedResultSetHandler.getRowValue(NestedResultSetHandler.java:
98)

org.apache.ibatis.executor.resultset.NestedResultSetHandler.applyNestedResultMappings(NestedResultSetHandler.java:
127)

org.apache.ibatis.executor.resultset.NestedResultSetHandler.getRowValue(NestedResultSetHandler.java:
98)

org.apache.ibatis.executor.resultset.NestedResultSetHandler.applyNestedResultMappings(NestedResultSetHandler.java:
127)
...
It looks like MyBatis can't handle recursive mapping. Can anyone clear
this up? Is it possible to write recursive mapping statements at all?

Thanks,
Andrew


On 24 дек, 03:08, c c <ele.test...@gmail.com> wrote:
> <select id="selectPeriod" parameterType="Long"
> resultMap="periodResultMap">
>        SELECT * FROM period WHERE id_period = #{id}
>        </select>
>
> this have some problem
>
> 2011/12/23 Bokie <jms.cer...@gmail.com>

Bokie

unread,
Dec 24, 2011, 5:27:31 PM12/24/11
to mybatis-user
For this to work you need to remove the "resultMap" attribute from the
"association" element like this:

<resultMap id="periodResultMap" type="Period">
<id property="id" column="id_period"/>
<result property="periodname" column="name"/>
<result property="kod" column="code"/>
<result property="begdate" column="begdate"/>
<result property="enddate" column="enddate"/>
<result property="flag" column="flag"/>
<association property="parent" column="par_id_period"
select="selectPeriod"/>
</resultMap>
<select id="selectPeriod" parameterType="Long"
resultMap="periodResultMap">
SELECT *
FROM period
WHERE id_period = #{id}
</select>


On Dec 22, 9:31 pm, demosfen <andrey.sha...@gmail.com> wrote:

demosfen

unread,
Dec 26, 2011, 12:18:48 PM12/26/11
to mybatis-user
Thank you, Bokie. I changed my example and it works now.
Reply all
Reply to author
Forward
0 new messages