2015-08-26 96 views
1

完成我的訓練應用程序的第一部分後,我面臨着這樣的錯誤:春「用戶」未映射

org.springframework.security.authentication.AuthenticationServiceException: user is not mapped [from user u where u.Login = :login] 

一旦我點擊登錄按鈕,被拋出。我不明白什麼是缺失,這是我LoginDAO和DAOImpl:

UserLoginDAOImpl:

package school.dao; 

import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

import school.model.User; 

import java.util.ArrayList; 
import java.util.List; 

@Repository 

public class UserLoginDAOImpl implements UserLoginDAO{ 
    @Autowired 
     private SessionFactory sessionFactory; 

     private Session openSession() { 
      return sessionFactory.getCurrentSession(); 
     } 

     public User getLogin(String Login) { 
      List<User> userList = new ArrayList<User>(); 
      Query query = openSession().createQuery("from user u where u.Login = :login"); 
      query.setParameter("Login", Login); 
      userList = query.list(); 
      if (userList.size() > 0) 
       return userList.get(0); 
      else 
       return null; 
     } 
} 

UserLoginDAO:

package school.dao; 

import school.model.User; 

public interface UserLoginDAO { 
    public User getLogin(String Login); 
} 

登錄頁面:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets"> 
<h:head> 

</h:head> 
<h:body> 
    <div align="right" style=""> 
     <h:form id="loginFormId" prependId="false"> 
      <div id="loginFieldsPnlId"> 
       <div id="loginFieldUsrContId"> 
        <h:outputText id="outTxtUserNameId" value="Username: " name="outTxtUserNameNm"></h:outputText> 
        <h:inputText id="userName" required="true" value="#{UserLoginMB.userName}" requiredMessage="Please enter username"></h:inputText> 
        <h:outputLabel id="outLblUserNameId" for="userName" name="outLblUserNameNm"></h:outputLabel> 
       </div> 
       <div id="loginFieldPassContId"> 
        <h:outputText id="outTxtPasswordId" value="Password: " name="outTxtPasswordNm"></h:outputText> 
        <h:inputSecret id="password" required="true" value="#{UserLoginMB.password}" requiredMessage="Please enter password" name="inTxtPasswordNm"></h:inputSecret> 
        <h:outputLabel id="outLblPasswordId" for="password" name="outLblPasswordNm"></h:outputLabel> 
       </div> 
      </div> 
      <div id="loginBtnPanelId"> 
       <h:commandButton id="btnLoginId" value="Login" action="#{UserLoginMB.login}" styleClass="loginPanelBtn"></h:commandButton> 
       <h:commandButton id="btnCancelId" value="Cancel" action="#{UserLoginMB.cancel}" styleClass="loginPanelBtn" immediate="true" update="loginFormId"></h:commandButton> 
      </div> 

     </h:form> 
    </div> 
    <div> 
     <h:messages></h:messages> 
    </div> 
</h:body> 
</html> 

確切Tomcat上的錯誤:

org.springframework.security.authentication.AuthenticationServiceException: user is not mapped [from user u where u.Login = :login] 
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:109) 
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132) 
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156) 
    at school.managedBeans.UserLoginBean.login(UserLoginBean.java:32) 
    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:497) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:245) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    at org.springframework.faces.webflow.FlowActionListener.processAction(FlowActionListener.java:71) 
    at org.springframework.faces.model.SelectionTrackingActionListener.processAction(SelectionTrackingActionListener.java:64) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    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:154) 
    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:199) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    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:262) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) 
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: user is not mapped [from user u where u.Login = :login] 
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180) 
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110) 
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93) 
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3252) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) 
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119) 
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214) 
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192) 
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537) 
    at school.dao.UserLoginDAOImpl.getLogin(UserLoginDAOImpl.java:26) 
    at school.service.CustomUserLoginService.loadUserByUsername(CustomUserLoginService.java:26) 
    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:497) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
    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.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
    at com.sun.proxy.$Proxy18.loadUserByUsername(Unknown Source) 
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:101) 
    ... 74 more 

編輯:添加用戶POJO

@Entity 
@Table (name="USER") 
public class User { 
    private int idUser; 
    private String NomUser; 
    private String PrenUser; 
    private String MailUser; 
    private String AdressUser; 
    private int PhoneUser; 
    private Date DateNaissanceUser; 
    private int AccountStatus; 
    private String Password; 
    private String Login; 
    private char SexeUser; 
    private String ImagePath; 
    private int idRole; 


    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name="IDUSER", unique = true, nullable = false) 
    public int getidUser() {  return idUser;  } 
    public void setidUser(int iduser) {  this.idUser = iduser; } 

    @Column(name="NOMUSER", nullable = false) 
    public String getNomUser() {  return NomUser; } 
    public void setNomUser(String nomUser) {  this.NomUser = nomUser; } 

    @Column(name="PRENUSER", nullable = false) 
    public String getPrenUser() { return PrenUser; } 
    public void setPrenUser(String prenUser) { this.PrenUser = prenUser; } 

    @Column(name="MAILUSER", nullable = false) 
    public String getMailUser() { return MailUser; } 
    public void setMailUser(String mailUser) { this.MailUser = mailUser; } 

    @Column(name="ADRESSUSER", nullable = false) 
    public String getAdressUser() {  return AdressUser;  } 
    public void setAdressUser(String adressUser) { this.AdressUser = adressUser; } 

    @Column(name="PHONEUSER", nullable = false) 
    public int getPhoneUser() {  return PhoneUser;  } 
    public void setPhoneUser(int phoneUser) { this.PhoneUser = phoneUser; } 

    @Column(name="DATENAISSANCEUSER", nullable = false) 
    public Date getDateNaissanceUser() {  return DateNaissanceUser;  } 
    public void setDateNaissanceUser(Date dateNaissanceUser) { this.DateNaissanceUser = dateNaissanceUser; } 

    @Column(name="ACCOUNTSTATUS", nullable = false) 
    public int getAccountStatus() {  return AccountStatus;  } 
    public void setAccountStatus(int accountStatus) { this.AccountStatus = accountStatus; } 

    @Column(name="PASSWORD", nullable = false) 
    public String getPassword() { return Password; } 
    public void setPassword(String password) { this.Password = password; } 

    @Column(name="LOGIN", nullable = false) 
    public String getLogin() { return Login;  } 
    public void setLogin(String login) {  this.Login = login; } 

    @Column(name="SEXEUSER", nullable = false) 
    public char getSexeUser() {  return SexeUser; } 
    public void setSexeUser(char sexeUser) {  this.SexeUser = sexeUser; } 

    @Column(name="IMAGEPATH", nullable = true) 
    public String getImagePath() { return ImagePath;  } 
    public void setImagePath(String imagePath) {  this.ImagePath = imagePath; } 

    @ManyToOne(cascade = CascadeType.ALL) 
    @JoinColumn(name="IDROLE", nullable=false) 
    public int getidRole() {  return idRole;  } 
    public void setidRole(int idRole) { this.idRole = idRole; } 
    private Roles roles; 
    public Roles getRoles() { 
     return roles; 
    } 
    public void setRoles(Roles roles) { 
     this.roles = roles; 
    } 
+0

你能告訴我們User POJO的代碼嗎? – ParagFlume

+0

Pojo?意味着什麼 !? –

+1

嘗試用''而不是'user'例如'Query query = openSession()。createQuery(「from User u where u.Login =:login」);' – ParagFlume

回答

1

大多數的答案和評論都表明了以下幾點:

Query query = openSession().createQuery("from User u where u.Login = :login"); 

我覺得這是你的誤解,也是你的bean的屬性沒有被正確映射。

getLogin和setLogin引用屬性「login」而不是「Login」 - 請考慮重新命名屬性爲「camelCased」並相應地調整setter/getter方法。

我用hsqldb + hibernate5 + Java8/JPA在本地運行了一個測試 - 在你的情況中 - 異常最有可能被翻譯,但其要點在於此。

  • 查詢 - 它是用戶不能用戶
  • where子句 - 屬性(根據JavaBeans Specification)應該是「登錄」,從訪問方法推斷setLogin和getLogin。
  • 將您的屬性名稱與JavaBeans Specification定義的「camelCase」語義對齊。
+0

它保持相同的錯誤: 引起: org.springframework.security.authentication.AuthenticationServiceException:用戶沒有映射[用戶u其中u.login =:登錄] –

+0

然後這最後一部分是你的Hibernate配置是不正確和/或不包含用戶類。請檢查該配置並確定您是否有/未映射到用戶類中。 –

+0

我補充說,我指的是在Github上裸露的例子 - 它使用Java8所以要認識到的是:https://github.com/djgraff209/question32217125 –

0

異常說錯誤,user類沒有映射

org.springframework.security.authentication.AuthenticationServiceException: user is not mapped [from **user** u where u.Login = :login] 

更新您的查詢是這樣的:

Query query = openSession().createQuery("from User u where u.Login = :login"); 

關鍵是user VS User

+1

我這樣做,結果是:相同的: org.springframework.security.authentication.AuthenticationServiceException:用戶沒有映射[從用戶U其中u.Login =:登錄] –

+1

HTTPS://docs.jboss .ORG /休眠/ ORM/3.6 /參考/ EN-US/HTML/queryhql.html#queryhql-casesensitivity – ParagFlume

+0

@ChaibiAlaa可能您發佈的所有堆棧跟蹤嗎? – gonzalon