MyBatis 引用嵌套对象属性导致 OgnlException
MyBatis referring nested object attribute resulting in OgnlException
我在使用 MyBatis SQL 查询时遇到了一些问题。这是我的对象 class:
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[=12=](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[=12=](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 查询中引用嵌套对象属性的任何想法?谢谢!
错误显示“getProperty(null, "roleID") 的来源为空”,这意味着 role
是 null
。
添加 null 检查应该可以解决问题。
<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>
在大多数情况下,您可以使用 <where />
而不是 <trim />
。
我在使用 MyBatis SQL 查询时遇到了一些问题。这是我的对象 class:
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[=12=](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[=12=](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 查询中引用嵌套对象属性的任何想法?谢谢!
错误显示“getProperty(null, "roleID") 的来源为空”,这意味着 role
是 null
。
添加 null 检查应该可以解决问题。
<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>
在大多数情况下,您可以使用 <where />
而不是 <trim />
。