jpa - EntityManager null pointer exception from Persistence Context Java EE -


i have problem using entitymanager managed ejb container.

in ear have 3 modules:

-domain - there entity classes , dao implementations interfaces ejbs.

-services - there have logic related processing requests, invoking bussiness exceptions based on libraries exceptions (for example database exceptions).

-rest services - there have rest endpoints receive , send business objects.

so main problem using ejbs in services layer, can declare rest endpoint class, service class, , dao class ejbs. way can't catch exceptions related database, because red transactionrollback exception in ejb container provided wildfly , exception existing on commiting transaction.

i thought removing @stateless annotation service class, when ejb container don't inject entitymanager in dao class. tried using @inject annotation in service class, still same nullpointer exception on entitymanager in dao class.

below adding sample code implementations in project :

dao class:

@stateless public class carddaoimpl implements carddao {  @persistencecontext(unitname = "test") protected entitymanager em;  public cardbase addcardtodatabase(cardbase card) {     em.persist(card);     return card; } 

service class (here want handle businessexceptions):

@stateless public class testservice implements testserviceif {  @ejb(beaninterface = carddao.class) private carddao dao;  @override public cardbase addcard(cardbase card) {     dao.addcardtodatabase(card);     return card; }  } 

rest endpoint class:

@stateless @path("/test") public class test {  @ejb(beaninterface = testserviceif.class) private testserviceif service;  @get @path("/test") @produces(mediatype.application_json) public string addcard() {     cardbase card = new samplecard();     //setting card object fields//     service.addcard(card);     return "hello"; } 

}

this 1 solution i've discovered. what's throwing businnesexceptions? maybe it's not practice throwing them in service layer?

with ejb 3.x, have specificities exceptions occurring in ejb methods.

-services - there have logic related processing requests, invoking bussiness exceptions based on libraries exceptions (for example database exceptions).

by default, container wraps in technical exception (ejbtransactionrolledbackexception) exceptions occurring in ejb considers unexpected. in same way can catch unexpected exceptions in application. beside exception wrapping, container triggers rollback on current transaction.
avoid technical exception wrapping container, business exceptions must not considered unexpected container.

broadly, have 2 cases :

  • either, these checked. nothing in case.

  • either, these runtime. must add applicationexception annotation on them or specify application-exception behavior since xml configuration of ejb.

from applicationexception javadoc :

: applied exception denote application exception , should reported client directly (i.e., unwrapped).

at last, rollback operation may desirable or not. may configure :

@applicationexception(rollback=true) public class mybusinessruntimeexception extends runtimeexception { 

or

@applicationexception(rollback=false) public class mybusinessruntimeexception extends runtimeexception { 

with weblogic, remember had declare these exceptions in signature of method.


update comment :

but first need catch exception third party lib example constraintvalueexception.

you have 2 ways of doing. handling exception explicitly problem may occur or handling exception in transverse way. think refer constraintviolationexception.

  • handling exception occurs when exception specific , needs context handled :

if expect specific third party exception in specific scenario, catch return business exception, should catch third party exception risen.
first, practice catch exception know how handle it.
second, avoid letting exception going next layer , therefore turned ejbtransactionrolledbackexception exception.
however, sometimes, exception type may handled in transversal way reasons.
in case, use transversal handling exception.

  • transverse handling when exception contains required information handled:

if want handle expected runtime exception in transversal way, may use jee interceptor , catching throw own business exception.
in case, must define in interceptor mapping between cause of caught exception , custom exception.

example of interceptor perform need (it doesn't handle scenarios, nominal scenario) :

public class wrappinginbusinessexceptioninterceptor {      @aroundinvoke     public object handleexceptions(final invocationcontext ctx) throws exception {          try {             object result = ctx.proceed();             return result;         }          catch (exception exception) {             // log root exception                ...                exception causeexception = e.getcause();                if (causeexception !=null &&                    causeexception.getclass()==constraintvalueexception.class){                   // throw custom exception                   string businessmsg =                            retrievebusinessmessagefrom(causeexception);                   throw new mybusinessexception(businessmsg);                 }         }                                     } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -