My Setup:

  • mongo-java-driver 2.11.3

  • spring-data-mongodb 1.4.0.M1

  • spring-data-commons 1.7.0.M1

  • querydsl-mongodb 3.3.0

  • Spring 季4.0.0.RELEASE

  • java 1.7

  • MongoDB v2.4.8

User.java:

@Document(collection="user")
public class User {
    public User(String username) {
        this.username = username;
    }

    @Id
    private ObjectId id;
    public ObjectId getId() { return id; }

    @Indexed(unique=true)
    private String username;
    public String getUsername() { return username; }

    /* more fields */
}

Profile.java:

@Document(collection="userProfile")
public class Profile {
    public Profile(User user) {
        this.user = user;
    }

    @Id 
    private ObjectId id;
    public ObjectId getId() { return id; }

    @DBRef @Indexed(unique=true)
    private User user;
    public User getUser() { return user; }

    /* more fields */
}

UserRepository:

public interface UserRepository extends MongoRepository<User, ObjectId>, QueryDslPredicateExecutor<User> {}

ProfileRepository:

public interface ProfileRepository extends MongoRepository<Profile, ObjectId>, QueryDslPredicateExecutor<Profile> {
    @Query("{ user.$id: ?0 }")
    Profile findByUserId(ObjectId userId);
}

生成 QUser:

@Generated("com.mysema.query.codegen.EntitySerializer")
public class QUser extends EntityPathBase<User> {

    private static final long serialVersionUID = 2145065918L;

    private static final PathInits INITS = PathInits.DIRECT2;

    public static final QUser user = new QUser("user");

    public final org.bson.types.QObjectId id;

    public final StringPath username = createString("username");

    /* more fields */

    public QUser(String variable) {
        this(User.class,  forVariable(variable), INITS);
    }

    public QUser(Path<? extends User> path) {
        this(path.getType(), path.getMetadata(), path.getMetadata().isRoot() ? INITS : PathInits.DEFAULT);
    }

    public QUser(PathMetadata<?> metadata) {
        this(metadata, metadata.isRoot() ? INITS : PathInits.DEFAULT);
    }

    public QUser(PathMetadata<?> metadata, PathInits inits) {
        this(User.class,  metadata, inits);
    }

    public QUser(Class<? extends User> type, PathMetadata<?> metadata, PathInits inits) {
        super(type, metadata, inits);
        this.id = inits.isInitialized("id") ? new org.bson.types.QObjectId(forProperty("id")) : null;
    }

}

生成 QProfile:

@Generated("com.mysema.query.codegen.EntitySerializer")
public class QProfile extends EntityPathBase<Profile> {

    private static final long serialVersionUID = -1321832234L;

    private static final PathInits INITS = PathInits.DIRECT2;

    public static final QProfile profile = new QProfile("profile");

    public final org.bson.types.QObjectId id;

    public final QUser user;

    /* more fields */

    public QProfile(String variable) {
        this(Profile.class,  forVariable(variable), INITS);
    }

    public QProfile(Path<? extends Profile> path) {
        this(path.getType(), path.getMetadata(), path.getMetadata().isRoot() ? INITS : PathInits.DEFAULT);
    }

    public QProfile(PathMetadata<?> metadata) {
        this(metadata, metadata.isRoot() ? INITS : PathInits.DEFAULT);
    }

    public QProfile(PathMetadata<?> metadata, PathInits inits) {
        this(Profile.class,  metadata, inits);
    }

    public QProfile(Class<? extends Profile> type, PathMetadata<?> metadata, PathInits inits) {
        super(type, metadata, inits);
        this.id = inits.isInitialized("id") ? new org.bson.types.QObjectId(forProperty("id")) : null;
        this.user = inits.isInitialized("user") ? new QUser(forProperty("user"), inits.get("user")) : null;
    }

}

What works:

profileRepository.findByUserId(someUser.getId());

userRepository.findOne(QUser.user.username.eq("someUsername"));

What doesn't work:

profileRepository.findOne(QProfile.profile.user.eq(someUser));

profileRepository.findOne(QProfile.profile.user.id(someUser.getId()));

Misc:

这个问题(QueryDsl MongoDb Relation)看起来与我的非常相似,并且在评论中创建了一些票证(https://github.com/mysema/querydsl/issues/583) . But 这张票是指缺少对 @Reference 注释的支持,这与问题本身无关?!

这有用吗?难道我做错了什么?或者我使用的是错误的版本?

TRACE LOG:

网址: PATCH http://localhost:8080/users/52d6eb4d03640c2f5a5ee726

被称为Java代码(在 UserServiceImpl 中):

myproject.domain.security.User u = userRepo.findOne(id);    
myproject.domain.security.Profile p = profileRepo.findOne(QProfile.profile.user.eq(u));
System.out.println(p.firstName);

这是日志:

/* some Spring Security stuff omitted */
2014-01-16 23:47:13,911 DEBUG o.s.s.w.access.intercept.FilterSecurityInterceptor: 215 - Authorization successful
2014-01-16 23:47:13,911 DEBUG o.s.s.w.access.intercept.FilterSecurityInterceptor: 227 - RunAsManager did not change Authentication object
2014-01-16 23:47:13,911 DEBUG  org.springframework.security.web.FilterChainProxy: 323 - /users/52d6eb4d03640c2f5a5ee726 reached end of additional filter chain; proceeding with original chain
2014-01-16 23:47:13,917 TRACE  org.springframework.web.servlet.DispatcherServlet:1013 - Bound request context to thread: SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.context.HttpSessionSecurityContextRepository$Servlet3SaveToSessionRequestWrapper@5c99a8b2]
2014-01-16 23:47:13,919 DEBUG  org.springframework.web.servlet.DispatcherServlet: 843 - DispatcherServlet with name 'dispatcher' processing PATCH request for [/webapp/users/52d6eb4d03640c2f5a5ee726]
2014-01-16 23:47:13,921 TRACE  org.springframework.web.servlet.DispatcherServlet:1098 - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@21068197] in DispatcherServlet with name 'dispatcher'
2014-01-16 23:47:13,921 DEBUG         o.s.w.s.m.m.a.RequestMappingHandlerMapping: 222 - Looking up handler method for path /users/52d6eb4d03640c2f5a5ee726
2014-01-16 23:47:13,924 TRACE         o.s.w.s.m.m.a.RequestMappingHandlerMapping: 266 - Found 1 matching mapping(s) for [/users/52d6eb4d03640c2f5a5ee726] : [{[/users/{id}],methods=[PATCH],params=[],headers=[],consumes=[],produces=[],custom=[]}]
2014-01-16 23:47:13,925 DEBUG         o.s.w.s.m.m.a.RequestMappingHandlerMapping: 229 - Returning handler method [org.springframework.http.ResponseEntity<java.lang.Void> myproject.web.security.UserController.patch(org.bson.types.ObjectId) throws org.springframework.data.crossstore.ChangeSetPersister$NotFoundException]
2014-01-16 23:47:13,925 DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 249 - Returning cached instance of singleton bean 'userController'
2014-01-16 23:47:13,926 TRACE  org.springframework.web.servlet.DispatcherServlet:1138 - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@5d95b457]
2014-01-16 23:47:13,938 TRACE   o.s.w.m.s.HandlerMethodArgumentResolverComposite:  90 - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@4ece93f2] supports [class org.bson.types.ObjectId]
2014-01-16 23:47:13,939 TRACE   o.s.w.m.s.HandlerMethodArgumentResolverComposite:  90 - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMapMethodArgumentResolver@27cc2b23] supports [class org.bson.types.ObjectId]
2014-01-16 23:47:13,940 TRACE   o.s.w.m.s.HandlerMethodArgumentResolverComposite:  90 - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver@4d241b3e] supports [class org.bson.types.ObjectId]
2014-01-16 23:47:13,947 TRACE       org.springframework.web.method.HandlerMethod: 130 - Invoking [UserController.patch] method with arguments [52d6eb4d03640c2f5a5ee726]
2014-01-16 23:47:13,961 DEBUG  o.springframework.data.mongodb.core.MongoTemplate:1446 - findOne using query: { "id" : { "$oid" : "52d6eb4d03640c2f5a5ee726"}} fields: null for class: class myproject.domain.security.User in collection: user
2014-01-16 23:47:13,964 DEBUG org.springframework.data.mongodb.core.MongoDbUtils: 109 - Getting Mongo Database name=[dev]
2014-01-16 23:47:13,965 DEBUG  o.springframework.data.mongodb.core.MongoTemplate:1859 - findOne using query: { "_id" : { "$oid" : "52d6eb4d03640c2f5a5ee726"}} in db.collection: dev.user
2014-01-16 23:47:14,121 TRACE    o.s.w.c.s.AnnotationConfigWebApplicationContext: 331 - Publishing event in Root WebApplicationContext: org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent[source={ "_id" : { "$oid" : "52d6eb4d03640c2f5a5ee726"} , "username" : "hansi42" , "password" : "$2a$10$b0lUEN/lQXIEl2GdusGigOKZrSmB1xnrBR9VK4pklqzpGD03CB5SC" , "locked" : false , "enabled" : true , "createdDate" : { "$date" : "2014-01-15T20:10:53.329Z"} , "lastModifiedDate" : { "$date" : "2014-01-15T20:10:53.329Z"} , "createdBy" : { "$ref" : "user" , "$id" : { "$oid" : "52d61b65fc144887e496963f"}} , "lastModifiedBy" : { "$ref" : "user" , "$id" : { "$oid" : "52d61b65fc144887e496963f"}} , "accountExpiresAt" : { "$date" : "2014-04-15T20:10:53.329Z"}}]
2014-01-16 23:47:14,123 DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 249 - Returning cached instance of singleton bean 'org.springframework.data.mongodb.core.mapping.event.AuditingEventListener'
2014-01-16 23:47:14,123 DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 249 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalScheduledAnnotationProcessor'
2014-01-16 23:47:14,139 TRACE    o.s.w.c.s.AnnotationConfigWebApplicationContext: 331 - Publishing event in Root WebApplicationContext: org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent[source=myproject.domain.security.User@4db5bba0]
2014-01-16 23:47:14,141 DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 249 - Returning cached instance of singleton bean 'org.springframework.data.mongodb.core.mapping.event.AuditingEventListener'
2014-01-16 23:47:14,142 DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 249 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalScheduledAnnotationProcessor'
2014-01-16 23:47:14,160 DEBUG org.springframework.data.mongodb.core.MongoDbUtils: 109 - Getting Mongo Database name=[dev]
2014-01-16 23:47:14,230 DEBUG    o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver: 134 - Resolving exception from handler [org.springframework.http.ResponseEntity<java.lang.Void> myproject.web.security.UserController.patch(org.bson.types.ObjectId) throws org.springframework.data.crossstore.ChangeSetPersister$NotFoundException]: java.lang.NullPointerException
2014-01-16 23:47:14,231 DEBUG        o.s.w.s.m.a.ResponseStatusExceptionResolver: 134 - Resolving exception from handler [org.springframework.http.ResponseEntity<java.lang.Void> myproject.web.security.UserController.patch(org.bson.types.ObjectId) throws org.springframework.data.crossstore.ChangeSetPersister$NotFoundException]: java.lang.NullPointerException
2014-01-16 23:47:14,232 DEBUG  o.s.w.s.m.support.DefaultHandlerExceptionResolver: 134 - Resolving exception from handler [org.springframework.http.ResponseEntity<java.lang.Void> myproject.web.security.UserController.patch(org.bson.types.ObjectId) throws org.springframework.data.crossstore.ChangeSetPersister$NotFoundException]: java.lang.NullPointerException
2014-01-16 23:47:14,232 TRACE  org.springframework.web.servlet.DispatcherServlet:1023 - Cleared thread-bound request context: SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.context.HttpSessionSecurityContextRepository$Servlet3SaveToSessionRequestWrapper@5c99a8b2]
2014-01-16 23:47:14,241 DEBUG  org.springframework.web.servlet.DispatcherServlet: 954 - Could not complete request
java.lang.NullPointerException: null
    at myproject.services.security.UserServiceImpl.updateById(UserServiceImpl.java:59) ~[UserServiceImpl.class:na]
    at myproject.web.security.UserController.patch(UserController.java:47) ~[UserController.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_45]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_45]
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) ~[spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:804) [spring-webmvc-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) [spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) [spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) [spring-security-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) [spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) [spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.42]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.42]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.42]
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.42]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.42]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) [tomcat-coyote.jar:7.0.42]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) [tomcat-coyote.jar:7.0.42]
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) [tomcat-coyote.jar:7.0.42]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_45]
    at java.lang.Thread.run(Thread.java:744) [na:1.7.0_45]
2014-01-16 23:47:14,242 TRACE    o.s.w.c.s.AnnotationConfigWebApplicationContext: 331 - Publishing event in WebApplicationContext for namespace 'dispatcher-servlet': ServletRequestHandledEvent: url=[/webapp/users/52d6eb4d03640c2f5a5ee726]; client=[0:0:0:0:0:0:0:1]; method=[PATCH]; servlet=[dispatcher]; session=[7AEFE0888849ACCA51C0E3E5F31FC0BD]; user=[hansi42]; time=[330ms]; status=[failed: java.lang.NullPointerException]
2014-01-16 23:47:14,243 TRACE    o.s.w.c.s.AnnotationConfigWebApplicationContext: 331 - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/webapp/users/52d6eb4d03640c2f5a5ee726]; client=[0:0:0:0:0:0:0:1]; method=[PATCH]; servlet=[dispatcher]; session=[7AEFE0888849ACCA51C0E3E5F31FC0BD]; user=[hansi42]; time=[330ms]; status=[failed: java.lang.NullPointerException]
2014-01-16 23:47:14,244 DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 249 - Returning cached instance of singleton bean 'org.springframework.data.mongodb.core.mapping.event.AuditingEventListener'
2014-01-16 23:47:14,244 DEBUG   o.s.b.factory.support.DefaultListableBeanFactory: 249 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalScheduledAnnotationProcessor'
2014-01-16 23:47:14,245 DEBUG o.s.s.web.context.SecurityContextPersistenceFilter:  97 - SecurityContextHolder now cleared, as request processing completed
Jan 16, 2014 11:47:14 PM org.apache.catalina.core.StandardWrapperValve invoke
Schwerwiegend: Servlet.service() for servlet [dispatcher] in context with path [/webapp] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at myproject.services.security.UserServiceImpl.updateById(UserServiceImpl.java:59)
    at myproject.web.security.UserController.patch(UserController.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    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:748)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:804)
    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 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.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    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.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    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:344)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
    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:502)
    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:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

NullPointerExceptions在调用 p.firstName 时发生,因为 p 为null,如果我使用 profileRepository.findByUserId(someUser.getId()).firstname 则不会发生这种情况 .