JPA EntityManager Nullpointer exception (which shouldn't be!?) -
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 @ line entitymanager.persist called, 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 , other eclipselink jpa jars in class path.
here 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 problem? can help? ive been figuring out of day now.
problem solved: shouldn't using resource local because running in app server. anyway. appreciated
when set transaction-type="resource_local"
in persistence.xml
config file, means (=your application's code) take care of creating entitymanager
, handle transactions yourself. in such case can demand injected underlying container entitymanagerfactory
, can using @persistenceunit
annotation. on such object call createentitymanager()
gives entitymanager
.
if want use entitymanager
supplied container must specify transaction-type="jta"
. annotate entitymanager
in code, , not care of creating, committing or rolling transaction. done container use jta
transaction , decide when create, commit or rollback it.
those 2 ways commonly known respectively application managed entitymanager , container managed entitymanager
you can find more details on how work each of 2 ways of handling transactions here or here
Comments
Post a Comment