我登录了jsp,
我正在使用Hibernate OneToOne注释,用于useradmin和wafat,
class useradmin for login如果wafat中的PK(useradmin)记录很好但是当wafat中没有PK(useradmin)记录时我得到了这个错误〜
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
这是我的ERD~
这是来自wafat表的记录,
这是来自用户表的记录,
这是useradmin类,
package org.ppbni.splatter.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.OneToOne;
import javax.persistence.CascadeType;
@SuppressWarnings("serial")
@Entity
@Table(name="user")
public class UserAdmin implements Serializable{
@Id
@GeneratedValue
@Column(name="no_dana", unique=true, nullable=false)
private String no_dana;
@Column(name="npp")
private String npp;
@Column(name="password")
private String password;
@OneToOne(mappedBy="useradmin", cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
private Wafat wafat;
/**Setter and Getter Methods**/
}
这是我的wafat课程,
package org.ppbni.splatter.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@SuppressWarnings("serial")
@Entity
@Table(name="wafat")
public class Wafat implements Serializable{
@Id
@Column(name="kode_wafat", unique=true, nullable=false)
private String kode_wafat;
@Id
@JoinColumn(name="no_dana")
private String no_dana;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="tgl_wafat")
@Column(name="usia_wafat")
private Date tgl_wafat;
@Column(name="usia_wafat")
private int usia_wafat;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="tgl_makam")
private Date tgl_makam;
@Column(name="tempat_makam")
private String tempat_makam;
@OneToOne
@MapsId
@JoinColumn(name="no_dana")
private UserAdmin useradmin;
/**Setter and Getter Methods**/
}
这是ny登录控制器,
@RequestMapping(value="/afterlogin.html", method = RequestMethod.GET)
public String directIndex(ModelMap model) {
User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String sessionUser = user.getUsername();
try {
UserAdmin dataUser = userService.get(sessionUser);
String userAuth = dataUser.getAuthority();
System.out.println(userAuth);
if(userAuth.equals("ROLE_ANGGOTA") || userAuth.equals("ROLE_PENGURUS")){
return "redirect:/user/index.html";
}
else if(userAuth.equals("ROLE_ADMIN")){
return "redirect:/admin/index.html";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
这个错误〜
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.ppbni.splatter.dao.impl.UserDaoImpl.get(UserDaoImpl.java:33)
at org.ppbni.splatter.service.impl.UserServiceImpl.get(UserServiceImpl.java:21)
at org.ppbni.splatter.web.controller.LoginController.directIndex(LoginController.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
这是我的一部分,
@SuppressWarnings("unchecked")
@Override
public UserAdmin get(String no_dana)throws Exception{
DetachedCriteria criteria = DetachedCriteria.forClass(UserAdmin.class).add(Restrictions.eq("no_dana", no_dana));
List<UserAdmin> userList = getHibernateTemplate().findByCriteria(criteria);
return userList.get(0);
}
任何帮助都会很愉快:)
答案 0 :(得分:2)
由于您要将Restrictions.eq("no_dana", no_dana)
添加到您的标准,因此可能没有用户匹配该限制。因此调用userList.get(0)
将导致所述异常:它意味着您尝试从零索引(大小0)中访问第一个索引(0),这显然无法工作。
要解决此问题,您需要检查userList
的大小,例如通过userList.isEmpty()
或userList.size() > 0
。
如果没有用户,您所做的事情取决于您的应用程序的其余部分:
null
,在这种情况下,这意味着用户尚未找到,您需要采取相应行动UserNotFoundException
之类的命名异常,然后你可以在调用者中处理它,或者将它一直传递给控制器并向用户显示一条适当的消息< / LI>
<强>更新强>
看起来您的映射是错误的。从编辑看起来应该使用UserAdmin
映射Wafat
和no_dana
。因此,我建议Wafat
是no_dana
作为连接列的关系的拥有方。这意味着wafat
中的UserAdmin
应使用@OneToOne(mappedBy="useradmin", cascade=CascadeType.ALL)
进行注释。如果没有wafat的用户可能也想添加optional=true
。
这样,当没有引用Wafat
实例的UserAdmin
时,wafat
字段在加载UserAdmin
时应为空。
顺便说一下,@PrimaryKeyJoinColumn
应该删除。
答案 1 :(得分:0)
undefined
在 List<UserAdmin> userList = getHibernateTemplate().findByCriteria(criteria);
return userList.get(0);
上添加支票:
List
在您的控制器上:
List<UserAdmin> userList = getHibernateTemplate().findByCriteria(criteria);
if(userList!=null && !userList.isEmpty()){
return userList.get(0);
}
// throw a custom Exception, IllegalArgumentException as an example here
throw new IllegalArgumentException("No user were found");
return null;