persistence@glassfish.java.net

Re: using toplink in CMP

From: Wonseok Kim <guruwons_at_gmail.com>
Date: Mon, 5 Mar 2007 09:32:39 +0900

Hi sud,
First of all, what you are trying to use is not CMP technology, it's Java
Persistence(aka. JPA) and it's totally different from CMP.
In order to inject EntityManager to a class, the class should be an Session
Bean(Stateless or Stateful). Java EE container doesn't inject anything on
non-managed POJO classes. So you'd better make your DAO stateless session
bean by putting @Stateless annotation on top of the class.

-Wonseok

On 3/4/07, sudhakar <sudhakar_at_co.brazos.tx.us> wrote:
>
> I'm attempting to use toplink in CMP within Glassfish and am unable to
> figure out the right coonfiguration. Here's what I have so far:
>
> 1. Defined a connection pool called MyPool in Glassfish admin console
> 2. Defined a jdbc resource called jdbc/MyDB that uses MyPool in
> Glassfish admin console
> 3. In my ejb project I created a src/META-INF/persistence.xml file that
> looks like below:
>
> <persistence>
> <persistence-unit name="myPU" transaction-type="JTA">
> <jta-data-source>jdbc/MyDB</jta-data-source>
> <properties>
> <property name="toplink.logging.level" value="FINEST" />
> </properties>
> </persistence-unit>
> </persistence>
>
> 4. I then created a PersonDAO class as below:
>
> public class PersonDAO {
>
> protected EntityManagerFactory emf;
>
> @PersistenceContext(unitName = "myPU")
> protected EntityManager em;
>
> public Person findById(int id) {
> return em.find(Person.class, id);
> }
> }
>
> When I attempt to use this I get a NPE at the em.find line in PersonDAO
>
> Caused by: java.lang.NullPointerException
> at test.PersonDAO.findById(PersonDAO.java:17)
>
> I was hoping someone can point in the right direction on how to get this
> working. It looks like the em object is null. Isn't CMP supposed to take
> care of creating this object using dependency injection?
>
> Thanks in advance for any help.
>
> -sud
>