persistence@glassfish.java.net

RE: best practice for generic facade?

From: Vladimir Perlov <vladperl_at_hotmail.com>
Date: Fri, 8 May 2009 01:53:18 +0000

I don't think that you need to create Facade for every particular entity.
Probably the following link will help you:
http://www.javaworld.com/javaworld/jw-04-2009/jw-04-lean-soa-with-javaee6.html

example from the article:

    public <T> T create(T t) {
        this.em.persist(t);
        this.em.flush();
        this.em.refresh(t);
        return t;
    }


From: gtjacobson_at_gmail.com
Date: Thu, 7 May 2009 17:18:26 +0200
To: persistence_at_glassfish.dev.java.net
Subject: best practice for generic facade?

Hi

I have a question regarding the "best practice" for using multiple stateless session beans to manage my entities. All of my entities extend GenericEntity, and need to have some database operations performed before being persisted or merged. Below is a snippet from my GenericEntityFacade:



@Statelesspublic class GenericEntityFacade implements GenericEntityFacadeLocal

{ @PersistenceContext

    EntityManager em; public void create( GenericEntity ge )

    { em.createQuery( .... generic stuff ....

        em.persist( ge ); }

}

I have many other facades for managing various entities, e.g. SaleFacade for Sales. My question is, what is the best way to get the other facades to utilize the GenericEntityFacade?





Option 1: embed GenericEntityFacade and EntityManager





@Stateless


public class SaleFacade implements SaleFacadeLocal


{


    @PersistenceContext

    EntityManager em;



    @EJB

    private GenericEntityFacadeLocal genericEntityFacade;



    public void createSale( Sale sale )

    {

        em.createQuery( .... sale stuff ....

        genericEntityFacade.create( sale );

    }

}





Option 2: embed GenericEntityFacade and use its EntityManager





@Stateless


public class SaleFacade implements SaleFacadeLocal


{


    @EJB

    private GenericEntityFacadeLocal genericEntityFacade;



    public void createSale( Sale sale )

    {

        genericEntityFacade.getEntityManager().createQuery( .... sale stuff ....

        genericEntityFacade.create( sale );

    }

}




Option 3: extend GenericEntityFacade and use the inherited EntityManager



@Stateless

public class SaleFacade extends GenericEntityFacade implements SaleFacadeLocal

{ public void createSale( Sale sale )
    {
        em.createQuery( .... sale stuff ....
        genericEntityFacade.create( sale );
    }
}

I'm currently using option 3. Any other recommendations or advice would be welcome!

Thanks
Gary



_________________________________________________________________
Hotmail® has ever-growing storage! Don’t worry about storage limits.
http://windowslive.com/Tutorial/Hotmail/Storage?ocid=TXT_TAGLM_WL_HM_Tutorial_Storage1_052009