Announcement Announcement Module
Collapse
No announcement yet.
Why An Exception On accountRepository? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why An Exception On accountRepository?

    The following line is in the HomeController class of the showcase sample.
    Code:
    		model.addAttribute(accountRepository.findAccountByUsername(currentUser.getName()));
    I am wondering why I get an exception
    org.springframework.dao.EmptyResultDataAccessExcep tion: Incorrect result size: expected 1, actual 0
    org.springframework.dao.support.DataAccessUtils.re quiredSingleResult(DataAccessUtils.java:71)
    org.springframework.jdbc.core.JdbcTemplate.queryFo rObject(JdbcTemplate.java:735)
    org.springframework.social.showcase.account.JdbcAc countRepository.findAccountByUsername(JdbcAccountR epository.java:56)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:318)
    org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:196)
    $Proxy36.findAccountByUsername(Unknown Source)
    when I have
    Code:
            Account current = accountRepository.findAccountByUsername(currentUser.getName());

  • #2
    It's had to say exactly why you get that exception, but I have a solid guess. Here are a few clues based on what does work in the showcase.

    Note that the Spring Social Showcase uses Spring Security for user authentication and authorization. Also note that in security.xml, all pages in the application require that the user be authenticated:

    Code:
    <intercept-url pattern="/**" access="isAuthenticated()"  />
    Therefore, there's no way you can arrive at the home page without first having signed in. Attempts to go to the homepage unauthenticated will result in a redirect to the sign in page. So, by the time the user actually hits HomeController, they've already signed in and the Principal object is already populated, ready to fetch the account for the current user.

    If your app isn't using Spring Security or if your app is allowing access to that line of code you show without authentication, then you'll likely get the exception you got.

    Comment

    Working...
    X