Fetchtype.Eager正在给出NullPointerException

时间:2015-08-24 18:50:42

标签: java hibernate jpa jboss

我正在使用hibernate JPA的双向OnetoMany关系。

我得到一个空指针异常,即使我正在使用fetchtype.Eager并且同一段代码在一个rest方法中工作但不在另一个方法中工作?

CODE

用户

public class Users implements Serializable {
     /** other fields  **/
     @OneToMany(cascade = CascadeType.ALL, mappedBy = "users", fetch = FetchType.EAGER)
     private Collection<Tokens> tokensCollection;

     /** getters and setters for other fields **/

    @XmlTransient
    public Collection<Tokens> getTokensCollection() {
        return tokensCollection;
    }

    public void setTokensCollection(Tokens token) {
        if (!token.getUser().equals(this.Userid)) {
             token.setUser(this);
        }
        this.tokensCollection.add(token);
    }
}

令牌

public class Tokens implements Serializable {
     /** other fields  **/

     @JoinColumn(name = "userid", referencedColumnName = "userid")
     @ManyToOne(optional = false, fetch = FetchType.EAGER)
     private Users users;


     /** getters and setters for other fields **/

    @XmlTransient
    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        if (!users.getTokensCollection().contains(this)) {
             users.getTokensCollection().add(this);
        }
        this.users = users;
    }
}

我有两个休息方法,一个有一个JMS队列来设置用户并在插入用户后插入一个临时令牌(不确定因为我在使用setter和getter时得到了NPE所以我试图首先插入用户然后创建一个令牌然后匹配它们,如下所示。另一个是在成功授权时插入令牌的身份验证点。如果这不是最佳实践或性能问题是毁灭性的,我愿意改变我设置实体的方式。

插入方法

   Users user = rfl.getUserByUsername(username);
   Tokens tkn = new Tokens();
   tkn.setJWT(token);
   tkn.setUsers(user);

   //not sure if this is making a difference cos I read somewhere that lazily loading is only impacted once you iterate over the collection but it doesn't make a difference cos a) im using eager and b) it works in one place but not the other

   Collection<Tokens> tokenss = user.getTokensCollection();
   for (Tokens token1 : tokenss) {
        System.out.println("User:" + token1.getUsers());
   }

   //rfl is my ejb facade responsible for entity management i.e. merge, persist, remove, find, flush
   rfl.edit(user);

LOG

2015-08-24 19:29:31,241 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401)) java.lang.NullPointerException

2015-08-24 19:29:31,241 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at ejb.Tokens.setUsers(Tokens.java:125)

2015-08-24 19:29:31,242 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at ejb.RegisterUserQueue.onMessage(RegisterUserQueue.java:72)

2015-08-24 19:29:31,242 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

2015-08-24 19:29:31,242 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

2015-08-24 19:29:31,243 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2015-08-24 19:29:31,243 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at java.lang.reflect.Method.invoke(Method.java:483)

2015-08-24 19:29:31,243 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)

2015-08-24 19:29:31,244 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,244 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)

2015-08-24 19:29:31,244 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)

2015-08-24 19:29:31,245 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,245 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:407)

2015-08-24 19:29:31,246 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:82)

2015-08-24 19:29:31,246 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:93)

2015-08-24 19:29:31,246 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)

2015-08-24 19:29:31,247 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,247 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)

2015-08-24 19:29:31,247 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)

2015-08-24 19:29:31,248 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,248 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)

2015-08-24 19:29:31,248 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,249 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:407)

2015-08-24 19:29:31,249 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:55)

2015-08-24 19:29:31,250 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:83)

2015-08-24 19:29:31,250 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,250 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)

2015-08-24 19:29:31,251 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,251 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)

2015-08-24 19:29:31,252 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,252 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)

2015-08-24 19:29:31,252 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)

2015-08-24 19:29:31,253 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,253 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)

2015-08-24 19:29:31,253 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,254 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:251)

2015-08-24 19:29:31,254 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:342)

2015-08-24 19:29:31,254 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239)

2015-08-24 19:29:31,255 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,255 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)

2015-08-24 19:29:31,255 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,256 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43)

2015-08-24 19:29:31,256 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,256 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:95)

2015-08-24 19:29:31,257 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,257 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64)

2015-08-24 19:29:31,258 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,258 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59)

2015-08-24 19:29:31,258 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,259 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)

2015-08-24 19:29:31,260 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,260 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55)

2015-08-24 19:29:31,261 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,262 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.component.messagedriven.MessageDrivenComponentDescription$5$1.processInvocation(MessageDrivenComponentDescription.java:211)

2015-08-24 19:29:31,262 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,263 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64)

2015-08-24 19:29:31,263 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,264 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326)

2015-08-24 19:29:31,264 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:448)

2015-08-24 19:29:31,264 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61)

2015-08-24 19:29:31,265 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,265 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326)

2015-08-24 19:29:31,266 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80)

2015-08-24 19:29:31,266 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,266 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)

2015-08-24 19:29:31,267 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:185)

2015-08-24 19:29:31,267 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:182)

2015-08-24 19:29:31,267 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

2015-08-24 19:29:31,268 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)

2015-08-24 19:29:31,268 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:73)

2015-08-24 19:29:31,269 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at ejb.RegisterUserQueue$$$view107.onMessage(Unknown Source)

2015-08-24 19:29:31,269 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

2015-08-24 19:29:31,269 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

2015-08-24 19:29:31,270 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2015-08-24 19:29:31,270 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at java.lang.reflect.Method.invoke(Method.java:483)

2015-08-24 19:29:31,270 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.inflow.MessageEndpointInvocationHandler.doInvoke(MessageEndpointInvocationHandler.java:139)

2015-08-24 19:29:31,271 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.jboss.as.ejb3.inflow.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:73)

2015-08-24 19:29:31,271 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at ejb.RegisterUserQueue$$$endpoint19.onMessage(Unknown Source)

2015-08-24 19:29:31,272 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.hornetq.ra.inflow.HornetQMessageHandler.onMessage(HornetQMessageHandler.java:321)

2015-08-24 19:29:31,272 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.hornetq.core.client.impl.ClientConsumerImpl.callOnMessage(ClientConsumerImpl.java:1116)

2015-08-24 19:29:31,272 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.hornetq.core.client.impl.ClientConsumerImpl.access$500(ClientConsumerImpl.java:56)

2015-08-24 19:29:31,273 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.hornetq.core.client.impl.ClientConsumerImpl$Runner.run(ClientConsumerImpl.java:1251)

2015-08-24 19:29:31,273 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at org.hornetq.utils.OrderedExecutorFactory$OrderedExecutor$1.run(OrderedExecutorFactory.java:104)

2015-08-24 19:29:31,273 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

2015-08-24 19:29:31,274 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

2015-08-24 19:29:31,274 ERROR [stderr] (Thread-357 (HornetQ-client-global-threads-934440401))   at java.lang.Thread.run(Thread.java:745)

P.S。我在这里选择了Eager,因为我假设一个用户不会有太多的令牌,我可能会改为Lazy,但我还有其他字段会被Lazily加载,所以这与这个问题无关。

P.P.S我知道用户已被插入,因为我有一行在tkn.setUser(newuser)其他方法之前记录获得用户的用户名

0 个答案:

没有答案