Skip to main content

IllegalArgumentException trying to inject EntityManager

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
mjremijan
Offline
Joined: 2003-07-18

I hava a simple Netbeans/Maven project and I'm using Netbeans 7.3.1 and Glassfish 4.0. I have a stateless session bean about as simple as you can get it:

@Stateless
public class MemberManagerBean implements MemberManager {
    @PersistenceUnit(unitName = "MyPersistenceUnit")
    private EntityManager em;

    public Member insert(Member u) {
        em.persist(u);
        return u;
    }
}

and I have a persistence.xml again about as simple as you can get it:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="MyPersistenceUnit" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/journalds</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

I have the Glassfish JDBC pool configured to connect to a PosgreSQL database. No problems connecting to the database from Glassfish.

When the EAR deploys, I know the application is successfully connecting to the PostgreSQL database through the JDBC pool because I see the database tables for my @Entitys get created. So I know the db connection is working.

Problem is when I try to call the EJB I get the following exception:

Caused by: javax.ejb.CreateException: Could not create stateless EJB
        at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:514)
        at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:97)
        at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:698)
        ... 38 more
Caused by: java.lang.IllegalStateException: Exception attempting to inject Env-Prop: org.ferris.ejb.MemberManagerBean/em@Field-Injectable Resource. Class name = org.ferris.ejb.MemberManagerBean Field name=em@java.lang.String@@@ into class org.ferris.ejb.MemberManagerBean: Can not set javax.persistence.EntityManager field org.ferris.ejb.MemberManagerBean.em to com.sun.enterprise.container.common.impl.EntityManagerFactoryWrapper

Any thoughts?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mjremijan
Offline
Joined: 2003-07-18

1am user error. Need to use @PersistenceContext not @PersistenceUnit