我有問題從數據庫中獲取數據並將其轉換爲我需要的類型。Hibernate:引起:java.lang.ClassCastException:[Ljava.lang.Object;不能轉換爲<package.class>
我嘗試使用Spring安全性從數據庫中泄漏用戶。 它的工作方式如下:我插入用戶名和密碼和方法loadUserByUserName(String userName)
從UserDetailsServiceImpl
類到數據庫和cheaks是用戶存在於數據庫中,如果是(用戶存在於數據庫中),然後從數據庫中獲取並授權用戶並呈現給他安全頁面。但問題是在我的DAO中,impl類的方法不能將它轉換爲需要我的對象 - App1User
我的實體類和我在登錄頁面上看到的消息是這樣的(原因:[Ljava.lang.Object;不能轉換爲app1.domain .App1User enter code here
):
我在DAOImpl類方法:
public App1User findUserByName(String userName) {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
List<App1User> listUser = null;
try {
String stringSQL = "select au.userId, au.firstName, au.lastName, au.middleName, au.username, au.password, au.userPosition from App1User au where au.username = :userNameArg";
Query query = session.createQuery(stringSQL);
query.setParameter("userNameArg", userName);
listUser = (List<App1User>)query.list();
session.flush();
session.clear();
transaction.commit();
} catch(HibernateException e) {
System.err.println(e);
if(transaction != null)
transaction.rollback();
} finally {
if(session != null)
session.close();
}
return (App1User)listUser.get(0);
}
我hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.default_schema">PUBLIC</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="/WEB-INF/hibernateConf/App1User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
的例外,我現在所擁有的:
SEVERE: An internal error occurred while trying to authenticate the user.
org.springframework.security.authentication.InternalAuthenticationServiceException: [Ljava.lang.Object; cannot be cast to app1.domain.App1User
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:125)
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:143)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:167)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:192)
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:93)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:217)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:120)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
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:502)
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:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to app1.domain.App1User
at app1.persistance.UserManageDAOImpl.findUserByName(UserManageDAOImpl.java:102)
at app1.web.authentification.UserDetailsServiceImpl.loadUserByUsername(UserDetailsServiceImpl.java:36)
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:114)
... 42 more
,也是我UserDetailServiceImpl類:
public UserDetails loadUserByUsername(String userName)
throws UsernameNotFoundException {
App1User listUser = userManageDAOIF.findUserByName(userName);
String password = null;
Collection<GrantedAuthority> authorities = null;
if(listUser != null) {
password = (String)listUser.getPassword();
authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
org.springframework.security.core.userdetails.User secureUser = new
org.springframework.security.core.userdetails.User(userName, password, authorities);
return secureUser;
} else {
throw new UsernameNotFoundException("No user exist in datbase");
}
}
這是halped。請告訴我這與HQL有關,hibernate如何從數據庫中選擇數據,我需要閱讀的內容,對吧? –
@java_user是的。你可以閱讀關於這個的Hibernate文檔。 –
這幫了我。我改變了SQL查詢,它工作完美。感謝@RohitJain – Ruthwik