2017-11-08 11 views
1

次の問題があります。私はJava EEサーバーとしてWildfly 11を使用しています。私は住所エンティティのリストを持つ顧客エンティティを持っています。私は顧客にアドレスのリストを設定しようとするとNullPointerExceptionを取得します。JPA Null Pointer @OneToManyを持つエンティティにエンティティのリストを追加するときに例外が発生する

マイアドレスエンティティ:

@Entity 
public class Address { 

@Id 
@GeneratedValue 
private long id; 

private String street; 
private String houseNumber; 

@ManyToOne 
@JoinColumn 
private Customer customer; 
-- constructor, getters, setters -- 

私の顧客が複数のアドレスを持つことができます。

@Entity 
public class Customer { 

@Id 
@GeneratedValue 
private long id; 

private String name; 

@OneToMany(mappedBy="customer") 
private List<Address> adresses; 
-- constructor, getters, setters -- 

私も住所と顧客を作成しようとするスタートアップBeanは、顧客にアドレスを追加していますアドレスリストを作成し、顧客を維持する。私はWildfly 11に私のプロジェクトを展開しようとすると、私はこの行でNullPointerExceptionが取得、今

@Startup 
@Singleton 
public class StartupBean { 

    @PersistenceContext 
    private EntityManager em; 

    @PostConstruct 
    private void init() { 
     Address address = new Address(); 
     address.setStreet("Example Street"); 
     address.setHouseNumber("123"); 

     Customer customer = new Customer(); 
     customer.setName("Bob"); 
     customer.getAdresses().add(address); 
     address.setCustomer(customer); 

     em.persist(customer); 
    } 

:ここ

は、それがどのように見えるかですcustomer.getAdresses().add(address);

ここでフルスタックトレースです: 01:00:26529 ERROR [org.jboss.msc.service.fail](ServerServiceスレッドプール

-- 210) MSC000001: Failed to start service jboss.deployment.subunit."my_project-ear.ear"."my_project-ejb-0.0.1-SNAPSHOT.jar".component.StartupBean.START: org.jboss.msc.service.StartException in service jboss.deployment.subunit."my_project-ear.ear"."my_project-ejb-0.0.1-SNAPSHOT.jar".component.StartupBean.START: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance 
    at org.jboss.as.ee.component.ComponentStartService$1.run(ComponentStartService.java:57) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 
    at java.lang.Thread.run(Thread.java:748) 
    at org.jboss.threads.JBossThread.run(JBossThread.java:320) 
Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance 
    at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:163) 
    at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:134) 
    at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:88) 
    at org.jboss.as.ejb3.component.singleton.SingletonComponent.getComponentInstance(SingletonComponent.java:124) 
    at org.jboss.as.ejb3.component.singleton.SingletonComponent.start(SingletonComponent.java:138) 
    at org.jboss.as.ee.component.ComponentStartService$1.run(ComponentStartService.java:54) 
    ... 6 more 
Caused by: javax.ejb.EJBException: java.lang.NullPointerException 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:188) 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277) 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:354) 
    at org.jboss.as.ejb3.tx.LifecycleCMTTxInterceptor.processInvocation(LifecycleCMTTxInterceptor.java:74) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.weld.injection.WeldInjectionContextInterceptor.processInvocation(WeldInjectionContextInterceptor.java:43) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:60) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.ejb3.component.singleton.StartupCountDownInterceptor.processInvocation(StartupCountDownInterceptor.java:25) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:53) 
    at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161) 
    ... 11 more 
Caused by: java.lang.NullPointerException 
    at com.example.test.businesslogic.StartupBean.init(StartupBean.java:34) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:96) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.doLifecycleInterception(Jsr299BindingsInterceptor.java:122) 
    at org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:111) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:509) 
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:73) 
    at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:89) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.weld.injection.WeldInjectionInterceptor.processInvocation(WeldInjectionInterceptor.java:53) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptorFactory.java:107) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.ee.component.AroundConstructInterceptorFactory$1.processInvocation(AroundConstructInterceptorFactory.java:28) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor.processInvocation(WeldInterceptorInjectionInterceptor.java:56) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.weld.interceptors.Jsr299BindingsCreateInterceptor.processInvocation(Jsr299BindingsCreateInterceptor.java:105) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422) 
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275) 
    ... 26 more 
+0

こんにちはジャックブラン、歓迎、StackOverflow。あなたが完全なスタックトレースを投稿できるなら、それは非常に役に立ちます。また、あなたの質問の最初の数行であなたの問題を述べることをお勧めします(そして、あなたがすでに終わったように最後にそれを繰り返します)。 – Blueriver

+2

こんにちは、あなたのsugesstionsに感謝します。質問を編集してスタックトレースを追加します –

答えて

1

あなたは、nullポインタ例外があることを取得追加しようとしているアドレス一覧が存在しないためです。最初に初期化する必要があります。また、ListはJavaのインターフェースです。 Listインタフェースの実装をインスタンス化する必要があります。

ので、代わりにaddress.setCustomer(customer);のあなたが持っている必要があります。

List<Address> addresses = new ArrayList<>(); 
    addresses.add(address); 
    customer.setAdresses(addresses); 

そして、別のものを。この方法では、のアドレスのエントリが保持されません。 お客様とします。そして、FOREIGN_KEYがアドレスのエンティティをマップした方法(ツアー@JoinColumn注釈は関係のAddress側にあります)にあるため、例外はありません。

両方のエンティティを正常に保持するには、このように両方のエンティティを永続化する必要があります。

em.persist(address); 
em.persist(customer); 

しかし、あなたは、Java EEを使用するので、あなたが存続する順序は、重要ではなく、アプリケーションサーバは、トランザクションの世話をするので、それは実際にあなたのコードの最後まで、トランザクションをコミットしません私はエンティティをこの順序で維持することがより論理的だと思います。

アドレスリストの@OneToManyアノテーションのcascade=CascadeType.ALLプロパティを使用すると、em.persist(address);を避けることができます。

だから、それは次のようになります。@OneToMany(mappedBy="customer",cascade=CascadeType.ALL)

この方法であなたはお客様を持続する場合、住所があまりにも保持されます。

関連する問題