Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have problem to fetch data from database and cast it to me needed type.

I try to cheak for the regestered user from database using Spring security. It works like: I insert userName and password and method loadUserByUserName(String userName) from UserDetailsServiceImpl class goes to database and cheaks for is the user exist in DB and if yes(user exist in DB) then fetch it from DB and authorize the user and render for him secure page. But problem is the in my DAO impl class method can't cast it to need me object - App1User my entity class and I see on login page message like this(Reason: [Ljava.lang.Object; cannot be cast to app1.domain.App1Userenter code here):

My method in DAOImpl class:

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);
    }

My 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>

The exception I have right now:

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

And also my UserDetailServiceImpl class:

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");
        }

    }
share|improve this question
up vote 3 down vote accepted

When you select specific columns from DB, the result you get is not the entity object, but an Object[], with each element of array holding different column values. So, what you're getting from query.list() method is: List<Object[]>. So, certainly when you cast list.get(0), to App1User, that will result in ClassCastException.

So, either you get the Object[] and build the App1User entity object like this:

// size of this array would be number of columns in select query
Object[] attr = listUser.get(0);
App1User appUser = new App1User(attr[0], attr[1], attr[2], ...);

Certainly not the best way. Better approach is to get a List<App1User> from db. For that just change the query from:

String stringSQL = "select au.userId, au.firstName, au.lastName, au.middleName, au.username, au.password, au.userPosition from App1User au where au.username = :userNameArg";

to:

String stringSQL = "select au from App1User au where au.username = :userNameArg";
share|improve this answer
    
This is halped. Please tell me it's related to HQL how hibernate select data from DB, that what I need to read, right? – java_user Apr 15 '15 at 17:07
1  
@java_user Yes. You can read the Hibernate docs regarding this. – Rohit Jain Apr 15 '15 at 17:08
    
This helped me. I changed the sql query and it worked perfectly. Thanks @RohitJain – Ruthwik Sep 1 '16 at 17:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.