当我试图读取与mybatis有多个子关系的记录时.. 它将始终抛出null异常;;;
这是我的设置
<resultMap id="UserMap" type="user">
<id column="id" property="id" javaType="string" />
<result column="pswd" property="pswd" javaType="string" />
<result column="name" property="name" javaType="string" />
<result column="useYn" property="useYn" javaType="string" />
<result column="useLimitedSearchYn" property="useLimitedSearchYn" javaType="string" />
<result column="email" property="email" javaType="string" />
<result column="emailUseYn" property="emailUseYn" javaType="string" />
<result column="registDatetime" property="registDatetime" javaType="date" />
<result column="updateDatetime" property="updateDatetime" javaType="date" />
<collection property="roles" column="{id=id}" notNullColumn="roleCode" javaType="list" ofType="userRole">
<id column="id" property="id" javaType="string" />
<id column="roleCode" property="roleCode" javaType="string" />
<result column="roleUseYn" property="roleUseYn" javaType="string" />
<result column="roleRegistDatetime" property="roleRegistDatetime" javaType="date" />
</collection>
</resultMap>
和查询
<select id="selectOne" resultType="user" resultMap="UserMap">
SELECT User.id, User.pswd, User.name, User.useYn, User.useLimitedSearchYn, User.email, User.emailUseYn, User.registDatetime, User.updateDatetime,
UserRole.roleCode, UserRole.roleRegistDatetime
FROM User
LEFT OUTER JOIN UserRole ON User.id = UserRole.id
WHERE User.id = #{id}
</select>
和目标表是..
create table User
(
id varchar(26) not null,
pswd varchar(40) not null,
name varchar(40) not null,
useYn char(1) default 'Y',
useLimitedSearchYn char(1) default 'N',
email varchar(80),
emailUseYn char(1) default 'N',
registDatetime datetime default CURRENT_TIMESTAMP,
updateDatetime datetime default CURRENT_TIMESTAMP,
primary key (id)
);
create table UserRole
(
id varchar(26) not null,
roleCode varchar(40) not null,
roleUseYn char(1) default 'N',
roleRegistDatetime datetime default CURRENT_TIMESTAMP,
primary key (id, roleCode)
);
INSERT INTO User (id, pswd, name, registDatetime, updateDatetime)
VALUES ('super', SHA('admin'), 'Admin', NOW(), NOW());
INSERT INTO UserRole (id, roleCode, roleUseYn, roleRegistDatetime)
VALUES ('super', 'ROLE_ADMIN', 'Y', NOW());
INSERT INTO UserRole (id, roleCode, roleUseYn, roleRegistDatetime)
VALUES ('super', 'ROLE_USER', 'Y', NOW());
和对象是......
@Alias("user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String pswd;
private String name;
private String email;
private String emailUseYn;
private String useLimitedSearchYn;
private String useYn;
private Date registDatetime;
private Date updateDatetime;
private List<UserRole> roles;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPswd() {
return pswd;
}
public void setPswd(String pswd) {
this.pswd = pswd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmailUseYn() {
return emailUseYn;
}
public void setEmailUseYn(String emailUseYn) {
this.emailUseYn = emailUseYn;
}
public String getUseYn() {
return useYn;
}
public void setUseYn(String useYn) {
this.useYn = useYn;
}
public String getUseLimitedSearchYn() {
return useLimitedSearchYn;
}
public void setUseLimitedSearchYn(String useLimitedSearchYn) {
this.useLimitedSearchYn = useLimitedSearchYn;
}
public Date getRegistDatetime() {
return registDatetime;
}
public void setRegistDatetime(Date registDatetime) {
this.registDatetime = registDatetime;
}
public Date getUpdateDatetime() {
return updateDatetime;
}
public void setUpdateDatetime(Date updateDatetime) {
this.updateDatetime = updateDatetime;
}
public List<UserRole> getRoles() {
return roles;
}
public void setRoles(List<UserRole> roles) {
this.roles = roles;
}
}
@Alias("userRole")
public class UserRole implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String roleCode;
private String roleUseYn;
private Date roleRegistDatetime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRoleCode() {
return roleCode;
}
public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
}
public String getRoleUseYn() {
return roleUseYn;
}
public void setRoleUseYn(String roleUseYn) {
this.roleUseYn = roleUseYn;
}
public Date getRoleRegistDatetime() {
return roleRegistDatetime;
}
public void setRoleRegistDatetime(Date roleRegistDatetime) {
this.roleRegistDatetime = roleRegistDatetime;
}
}
使用版本是......
compile "mysql:mysql-connector-java:5.1.39"
compile "org.mybatis:mybatis:3.4.1"
compile "org.mybatis:mybatis-spring:1.3.0"
使用此设置会抛出这样的异常..
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NullPointerException
### The error may exist in /persistence/mysql/mybatis/mapper/UserMapper.xml
### The error may involve persistence.mysql.mybatis.mapper.UserMapper.selectOne
### The error occurred while handling results
### SQL: SELECT User.id, User.pswd, User.name, User.useYn, User.useLimitedSearchYn, User.email, User.emailUseYn, User.registDatetime, User.updateDatetime, UserRole.roleCode, UserRole.roleRegistDatetime FROM User LEFT OUTER JOIN UserRole ON User.id = UserRole.id WHERE User.id = ?
### Cause: java.lang.NullPointerException
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:79)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
at com.sun.proxy.$Proxy26.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:167)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy29.selectOne(Unknown Source)
at sample.dao.UserDao.selectOne(UserDao.java:29)
at sample.dao.UserDao$$FastClassBySpringCGLIB$$48655e9a.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
at sample.profiling.ProfilingAspect.profileDao(ProfilingAspect.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at sample.dao.UserDao$$EnhancerBySpringCGLIB$$4dce2a87.selectOne(<generated>)
at sample.service.UserService.getUser(UserService.java:27)
at sample.service.UserService$$FastClassBySpringCGLIB$$252104e0.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
at sample.profiling.ProfilingAspect.profileService(ProfilingAspect.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at sample.service.UserService$$EnhancerBySpringCGLIB$$6a14231c.getUser(<generated>)
at test.service.TestUserService.testGetUser(TestUserService.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:70)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NullPointerException
### The error may exist in /persistence/mysql/mybatis/mapper/UserMapper.xml
### The error may involve persistence.mysql.mybatis.mapper.UserMapper.selectOne
### The error occurred while handling results
### SQL: SELECT User.id, User.pswd, User.name, User.useYn, User.useLimitedSearchYn, User.email, User.emailUseYn, User.registDatetime, User.updateDatetime, UserRole.roleCode, UserRole.roleRegistDatetime FROM User LEFT OUTER JOIN UserRole ON User.id = UserRole.id WHERE User.id = ?
### Cause: java.lang.NullPointerException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
... 77 more
Caused by: java.lang.NullPointerException
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getPropertyMappingValue(DefaultResultSetHandler.java:455)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyPropertyMappings(DefaultResultSetHandler.java:424)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:860)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForNestedResultMap(DefaultResultSetHandler.java:825)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:311)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:286)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:183)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.ReuseExecutor.doQuery(ReuseExecutor.java:60)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 84 more
但是
compile "mysql:mysql-connector-java:5.1.39"
compile "org.mybatis:mybatis:3.2.8"
compile "org.mybatis:mybatis-spring:1.2.3"
它工作正常并返回我想要的结果。 有什么我错过的吗??
感谢阅读...
答案 0 :(得分:0)
它来自
column="{id=id}"
from collection element&#34; column =&#34; {property = column}&#34;&#34;
但我认为有必要匹配后续数据。
没有这个如何匹配后续数据并收集这些东西?