You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by davshowhan <da...@gmail.com> on 2015/03/21 11:27:50 UTC

Nullpointer on use of EntityManager in app run in TOMEE

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_MYDB"/>
         <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>




In EJB:

@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( );

	  }
}




Nullpointer happens on call of entitymanager.persist because entitymanager
is null.....but thats not suppose to be right? it should be not null.

Can anyone help me on this?

libraries in project:
eclipselink.jar
eclipselink-jpa jars
hsqldb.jar





--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Nullpointer-on-use-of-EntityManager-in-app-run-in-TOMEE-tp4674140.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Nullpointer on use of EntityManager in app run in TOMEE

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Le 21 mars 2015 12:54, "davshowhan" <da...@gmail.com> a écrit :
>
>
> What do you mean? is it wrong?
>

Use the entitymanagerfactory not the entitymanager

> RESOURCE-LOCAL is when you dont use a datasource right?
>

No, when you dont use jta and handle yourself transactions

>
>
> --
> View this message in context:
http://tomee-openejb.979440.n4.nabble.com/Nullpointer-on-use-of-EntityManager-in-app-run-in-TOMEE-tp4674140p4674142.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Nullpointer on use of EntityManager in app run in TOMEE

Posted by davshowhan <da...@gmail.com>.
What do you mean? is it wrong?

RESOURCE-LOCAL is when you dont use a datasource right?



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Nullpointer-on-use-of-EntityManager-in-app-run-in-TOMEE-tp4674140p4674142.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Nullpointer on use of EntityManager in app run in TOMEE

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Resource local means you can get a persistenceunit (factory)
Le 21 mars 2015 11:39, "davshowhan" <da...@gmail.com> a écrit :

> 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_MYDB"/>
>          <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>
>
>
>
>
> In EJB:
>
> @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( );
>
>           }
> }
>
>
>
>
> Nullpointer happens on call of entitymanager.persist because entitymanager
> is null.....but thats not suppose to be right? it should be not null.
>
> Can anyone help me on this?
>
> libraries in project:
> eclipselink.jar
> eclipselink-jpa jars
> hsqldb.jar
>
>
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Nullpointer-on-use-of-EntityManager-in-app-run-in-TOMEE-tp4674140.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.
>