【问题标题】:MyBatis referring nested object attribute resulting in OgnlExceptionMyBatis 引用嵌套对象属性导致 OgnlException
【发布时间】:2020-11-02 04:55:52
【问题描述】:

我在使用 MyBatis SQL 查询时遇到了一些问题。这是我的对象类:

public class UserRoleTO extends BaseObject {
    private String nric;
    private String unit;
    private String name;
    private String rankCode;
    private String rank;
    private String status;
    private RoleTO role;
    private Date dteUpdated = null;
    private String dateUpdated = null;
    private String updatedByNric = null;
    private String updatedByName = null;
    private String unitDesc = null;

    private MenuTO menu = null;
    private ArrayList accessRights = null;

    // setters and getters
}

还有我的 MyBatis SQL 查询:

<select id="getUserRole" resultMap="userRoleResult"
    parameterType="sc.securityMgt.integration.to.UserRoleTO">
    SELECT
    NRIC,
    NAME,
    ROLE_ID,
    UNIT_CODE,
    DATE_UPDATED,
    UPDATED_BY,
    UPDATED_BY_NAME,
    DEFUNCT_IND
    FROM
    USER_ROLE
    <trim prefix="WHERE" prefixOverrides="AND">
        <if test="nric != null">USER_ROLE.NRIC = #{nric} </if>
        <if test="role.roleID != null">AND USER_ROLE.ROLE_ID = #{role.roleID} </if>
    </trim>     
</select>

我打印了我的roleID,我打印了我的roleID值,它是null,但是在这种情况下,它既然是null,它不应该不附加WHERE子句吗?我收到错误消息:

Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'role.roleID != null'. Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "roleID")
    at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:48)
    at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)
    at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32)
    at org.apache.ibatis.scripting.xmltags.TrimSqlNode.apply(TrimSqlNode.java:55)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32)
    at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39)
    at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:305)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:87)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
    ... 47 more
Caused by: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "roleID")
    at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:3331)
    at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:121)
    at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
    at org.apache.ibatis.ognl.ASTChain.getValueBody(ASTChain.java:141)
    at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
    at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50)
    at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
    at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:560)
    at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:524)
    at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46)
    ... 66 more

关于如何在 MyBatis SQL 查询中引用嵌套对象属性的任何想法?谢谢!

【问题讨论】:

    标签: sql exception mybatis


    【解决方案1】:

    错误提示“getProperty(null, "roleID") 的源为 null”,这意味着 rolenull

    添加空检查应该可以解决问题。

    <where>
      <if test="nric != null">USER_ROLE.NRIC = #{nric}</if>
      <if test="role != null and role.roleID != null">AND USER_ROLE.ROLE_ID = #{role.roleID} </if>
    </where>
    

    在大多数情况下,您可以使用&lt;where /&gt; 而不是&lt;trim /&gt;

    【讨论】:

    猜你喜欢
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多