users@glassfish.java.net

EnitiyManager won't inject

From: Garth Keesler <garthk_at_gdcjk.com>
Date: Mon, 14 Jan 2008 08:35:28 -0600
I am new to Glassfish/NetBeans having switched from being new to Geronimo/Eclipse. So far I much prefer this combo. However, I have been working thru various code samples and using O'Reilly's EJB 3.0 book on two different platforms: Ubuntu 7.10/Postgres and XP/SQL Server with the same result. No EntityManager ever gets injected in the session bean that attempts to manipulate the persisted entity bean. I have tested both jdbc connections is simple SE programs so I know the connections are valid. Also, I generate the entity beans from the schema so that also verifies the connection (I think).

The steps I use on both systems are as follows:

  1. Create a new Enterprise App
  2. Create a new Persistence Unit on the EJB module
  3. Create a Database Schema from the database
  4. Create an entity bean from the schema using the Persistence, Create Entity Bean from Schema"
  5. Create a session (local) bean using the Persistance Session beans from existing cleasses
  6. Create a JUnit test from the <entity>facade bean
  7. Add a line to check that the EnitiyManager is null (it always is)
That's about it. I've even changed the @PersistenceContext line to includ (unitName="<name of persistence unit>") and it still fails.

System info:

Ubuntu 7.10 x86
JDK 1.6.0_04
Glassfish 2.0
Netbeans 6.0

All software fully upgraded/updated.

Since this is happening in both implementations (XP and Linux), it is obviously something I'm doing wrong. Below is the code for pretty much the entire project. This code is entirely auto-generated within Netbeans with no code changes. Let me know if I need to send more info.

Thanx,
Garth

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="PostPersist-ejbPU" transaction-type="JTA">
    <jta-data-source>NewOES</jta-data-source>
    <properties/>
  </persistence-unit>
</persistence>

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package OESPkg;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author garthk
 */
@Entity
@Table(name = "vendors")
@NamedQueries({@NamedQuery(name = "Vendors.findByRid", query = "SELECT v FROM Vendors v WHERE v.rid = :rid"), @NamedQuery(name = "Vendors.findByVendorname", query = "SELECT v FROM Vendors v WHERE v.vendorname = :vendorname"), @NamedQuery(name = "Vendors.findByVendorid", query = "SELECT v FROM Vendors v WHERE v.vendorid = :vendorid"), @NamedQuery(name = "Vendors.findByAddress1", query = "SELECT v FROM Vendors v WHERE v.address1 = :address1"), @NamedQuery(name = "Vendors.findByUserid", query = "SELECT v FROM Vendors v WHERE v.userid = :userid"), @NamedQuery(name = "Vendors.findByAddress2", query = "SELECT v FROM Vendors v WHERE v.address2 = :address2"), @NamedQuery(name = "Vendors.findByAddress3", query = "SELECT v FROM Vendors v WHERE v.address3 = :address3"), @NamedQuery(name = "Vendors.findByCity1", query = "SELECT v FROM Vendors v WHERE v.city1 = :city1"), @NamedQuery(name = "Vendors.findByCity2", query = "SELECT v FROM Vendors v WHERE v.city2 = :city2"), @NamedQuery(name = "Vendors.findByRegion", query = "SELECT v FROM Vendors v WHERE v.region = :region"), @NamedQuery(name = "Vendors.findByCountry", query = "SELECT v FROM Vendors v WHERE v.country = :country"), @NamedQuery(name = "Vendors.findByMailcode", query = "SELECT v FROM Vendors v WHERE v.mailcode = :mailcode")})
public class Vendors implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "rid", nullable = false)
    private Integer rid;
    @Column(name = "vendorname", nullable = false)
    private String vendorname;
    @Column(name = "vendorid", nullable = false)
    private String vendorid;
    @Column(name = "address1")
    private String address1;
    @Column(name = "userid")
    private String userid;
    @Column(name = "address2")
    private String address2;
    @Column(name = "address3")
    private String address3;
    @Column(name = "city1")
    private String city1;
    @Column(name = "city2")
    private String city2;
    @Column(name = "region")
    private String region;
    @Column(name = "country")
    private String country;
    @Column(name = "mailcode")
    private String mailcode;

    public Vendors()
      {
      }

    public Vendors(Integer rid)
      {
        this.rid = rid;
      }

    public Vendors(Integer rid, String vendorname, String vendorid)
      {
        this.rid = rid;
        this.vendorname = vendorname;
        this.vendorid = vendorid;
      }

    public Integer getRid()
      {
        return rid;
      }

    public void setRid(Integer rid)
      {
        this.rid = rid;
      }

    public String getVendorname()
      {
        return vendorname;
      }

    public void setVendorname(String vendorname)
      {
        this.vendorname = vendorname;
      }

    public String getVendorid()
      {
        return vendorid;
      }

    public void setVendorid(String vendorid)
      {
        this.vendorid = vendorid;
      }

    public String getAddress1()
      {
        return address1;
      }

    public void setAddress1(String address1)
      {
        this.address1 = address1;
      }

    public String getUserid()
      {
        return userid;
      }

    public void setUserid(String userid)
      {
        this.userid = userid;
      }

    public String getAddress2()
      {
        return address2;
      }

    public void setAddress2(String address2)
      {
        this.address2 = address2;
      }

    public String getAddress3()
      {
        return address3;
      }

    public void setAddress3(String address3)
      {
        this.address3 = address3;
      }

    public String getCity1()
      {
        return city1;
      }

    public void setCity1(String city1)
      {
        this.city1 = city1;
      }

    public String getCity2()
      {
        return city2;
      }

    public void setCity2(String city2)
      {
        this.city2 = city2;
      }

    public String getRegion()
      {
        return region;
      }

    public void setRegion(String region)
      {
        this.region = region;
      }

    public String getCountry()
      {
        return country;
      }

    public void setCountry(String country)
      {
        this.country = country;
      }

    public String getMailcode()
      {
        return mailcode;
      }

    public void setMailcode(String mailcode)
      {
        this.mailcode = mailcode;
      }

    @Override
    public int hashCode()
      {
        int hash = 0;
        hash += (rid != null ? rid.hashCode() : 0);
        return hash;
      }

    @Override
    public boolean equals(Object object)
      {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Vendors))
          {
            return false;
          }
        Vendors other = (Vendors) object;
        if ((this.rid == null && other.rid != null) || (this.rid != null && !this.rid.equals(other.rid)))
          {
            return false;
          }
        return true;
      }

    @Override
    public String toString()
      {
        return "OESPkg.Vendors[rid=" + rid + "]";
      }

}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package OESPkg;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author garthk
 */
@Stateless
public class VendorsFacade implements VendorsFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Vendors vendors)
      {
        em.persist(vendors);
      }

    public void edit(Vendors vendors)
      {
        em.merge(vendors);
      }

    public void remove(Vendors vendors)
      {
        em.remove(em.merge(vendors));
      }

    public Vendors find(Object id)
      {
        return em.find(OESPkg.Vendors.class, id);
      }

    public List<Vendors> findAll()
      {
        return em.createQuery("select object(o) from Vendors as o").getResultList();
      }

}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package OESPkg;

import java.util.List;
import javax.ejb.Local;

/**
 *
 * @author garthk
 */
@Local
public interface VendorsFacadeLocal {

    void create(Vendors vendors);

    void edit(Vendors vendors);

    void remove(Vendors vendors);

    Vendors find(Object id);

    List<Vendors> findAll();

}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package OESPkg;

import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author garthk
 */
public class VendorsFacadeTest {

    public VendorsFacadeTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception
      {
      }

    @AfterClass
    public static void tearDownClass() throws Exception
      {
      }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of create method, of class VendorsFacade.
     */
    @Test
    public void create()
      {
        System.out.println("create");
        Vendors vendors = null;
        VendorsFacade instance = new VendorsFacade();
        instance.create(vendors);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
      }

    /**
     * Test of edit method, of class VendorsFacade.
     */
    @Test
    public void edit()
      {
        System.out.println("edit");
        Vendors vendors = null;
        VendorsFacade instance = new VendorsFacade();
        instance.edit(vendors);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
      }

    /**
     * Test of remove method, of class VendorsFacade.
     */
    @Test
    public void remove()
      {
        System.out.println("remove");
        Vendors vendors = null;
        VendorsFacade instance = new VendorsFacade();
        instance.remove(vendors);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
      }

    /**
     * Test of find method, of class VendorsFacade.
     */
    @Test
    public void find()
      {
        System.out.println("find");
        Object id = null;
        VendorsFacade instance = new VendorsFacade();
        Vendors expResult = null;
        Vendors result = instance.find(id);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
      }

    /**
     * Test of findAll method, of class VendorsFacade.
     */
    @Test
    public void findAll()
      {
        System.out.println("findAll");
        VendorsFacade instance = new VendorsFacade();
        List<Vendors> expResult = null;
        List<Vendors> result = instance.findAll();
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
      }