0

My ejb code:

@Stateless
public class EmployeeBean {

      @PersistenceContext(unitName="Eclipselink_JPA")
      private EntityManager entitymanager;


      public void createEmployee(){
          Employee employee = new Employee( ); 
          employee.setEid( 1201 );
          employee.setEname( "Gopal" );
          employee.setSalary( 40000 );
          employee.setDeg( "Technical Manager" );

          entitymanager.persist( employee );
          entitymanager.getTransaction( ).commit( );

          entitymanager.close( );

      }
}

Basically, nullpointer exception happens at the line where entitymanager.persist is called, which should not happen right?

My persistence.xml file

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.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_2_0.xsd">

   <persistence-unit name="Eclipselink_JPA" transaction-type="RESOURCE_LOCAL">

      <class>com.jpa.beans.Employee</class>

      <properties>
         <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:E:\HQLDB_AJ"/>
         <property name="javax.persistence.jdbc.user" value="sa"/>
         <property name="javax.persistence.jdbc.password" value="password"/>
         <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver"/>
         <property name="eclipselink.logging.level" value="FINE"/>
         <property name="eclipselink.ddl-generation" value="create-tables"/>
      </properties>

   </persistence-unit>
</persistence>

I have eclipselink.jar and other eclipselink jpa jars in my class path.

Here is my entity beans:

@Entity
@Table
public class Employee {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)  

   private int eid;
   private String ename;
   private double salary;
   private String deg;

   public Employee(int eid, String ename, double salary, String deg) {
      super( );
      this.eid = eid;
      this.ename = ename;
      this.salary = salary;
      this.deg = deg;
   }

   public Employee( ) {
      super();
   }

   public int getEid( ) {
      return eid;
   }

   public void setEid(int eid) {
      this.eid = eid;
   }

   public String getEname( ) {
      return ename;
   }

   public void setEname(String ename) {
      this.ename = ename;
   }

   public double getSalary( ) {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   public String getDeg( ) {
      return deg;
   }

   public void setDeg(String deg) {
      this.deg = deg;
   }

   @Override
   public String toString() {
      return "Employee [eid=" + eid + ", ename=" + ename + ", salary=" + salary + ", deg=" + deg + "]";
   }
}

Whats seems to be the problem? can anyone help? ive been figuring this out of a day now.

Problem solved: i shouldn't be using resource local because i was running it in an app server. thanks for the help anyway. well appreciated

2 Answers2

4

When you set transaction-type="RESOURCE_LOCAL" in persistence.xml config file, it means that YOU (=your application's code) will take care of creating EntityManager and also will handle transactions yourself. In such case what you can demand to be injected by your underlying container is EntityManagerFactory and you can do it by using @PersistenceUnit annotation. On such object you simply call createEntityManager() that gives you EntityManager.

If you want to use EntityManager supplied by the container you must specify transaction-type="JTA". Then you annotate EntityManager exactly as in your code, and do not care of creating, committing or rolling back a transaction. It will be done by the container that will use JTA transaction and decide when to create, commit or rollback it.

Those two ways are commonly known as respectively Application Managed EntityManager and Container Managed EntityManager

You can find more details on how to work with each of two ways of handling transactions here or here

MateuszPrzybyla
  • 897
  • 8
  • 17
0

You dont have <provider> in your persistence.xml.. Try adding it.

Ex:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> or <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> or <provider>org.hibernate.ejb.HibernatePersistence</provider> depending on your container.

HTH

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
  • thanks ill try. is the "org.eclipse.persistence.jpa.PersistenceProvider" built-in in eclipse? no need to add an additional jar in my class path? – lecarpetron dookmarion Mar 21 '15 at 11:19
  • no.. it is implemented by EclipseLink.. you might need to add the jar or maven dependency ``` org.eclipse.persistence org.eclipse.persistence.jpa 2.6.0 ``` – karthik manchala Mar 21 '15 at 11:26