JPAController: EntityManagerFactory is null

View: New views
3 Messages — Rating Filter:   Alert me  

JPAController: EntityManagerFactory is null

by balta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,



today I tried my firts time to use JPA. I followed the assistents from Netbeans, created a simple entity, and then a JPAController for this. Netbeans created an class with the following attributes:




Code:
    @Resource

    private UserTransaction utx = null;

    @PersistenceUnit(unitName = "JavaEE-Test-ejbPU")

    private EntityManagerFactory emf = null;





But it wasn't working, because both were null. Should the annotations inject something here or do I have to touch the auto-generated class? I created a constructor with the following code:




Code:
        try {

            InitialContext ctx = new InitialContext();

            this.utx = (UserTransaction) ctx.lookup("java:comp/UserTransaction");

        } catch (NamingException ex) {

            Logger.getLogger(VisitorJpaController.class.getName()).log(Level.SEVERE, null, ex);

        }





So utx is working, but how do I have to set emf? Or am I doing something totally wrong?



Greets





JPAController: EntityManagerFactory is null

by balta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can nobody help? :(



Today I tried to directly use an EntityManager in my SessionBean, so I added the following attributes:




Code:
    @PersistenceUnit(unitName = "JavaEE-Test-ejbPU")

    private EntityManagerFactory emf;



    @PersistenceContext(unitName = "JavaEE-Test-ejbPU")

    private EntityManager em;





But also, both are null. How can this happen? Is it possible that glassfish can't find my PersisenceInit? And why?





Re: JPAController: EntityManagerFactory is null

by John Yeary :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Please see the difference

http://javaevangelist.blogspot.com/2006/10/persistence-unit-or-persistence.html

On Wed, Oct 21, 2009 at 4:45 AM, homer <wittemar@...> wrote:
Can nobody help? :(



Today I tried to directly use an EntityManager in my SessionBean, so I added the following attributes:




Code:
   @PersistenceUnit(unitName = "JavaEE-Test-ejbPU")

   private EntityManagerFactory emf;



   @PersistenceContext(unitName = "JavaEE-Test-ejbPU")

   private EntityManager em;





But also, both are null. How can this happen? Is it possible that glassfish can't find my PersisenceInit? And why?







--
John Yeary
--
http://javaevangelist.blogspot.com

"Far better it is to dare mighty things, to win glorious triumphs, even though checkered by failure, than to take rank with those poor spirits who neither enjoy much nor suffer much, because they live in the grey twilight that knows not victory nor defeat."
-- Theodore Roosevelt