jsf 2 - Using JPA2 in Tomcat 6: @PersitenceContext doesn't work, EntityManager is null -
i using jsf2 pure jpa2. problem entitymanager,
@persistencecontext private entitymanager entitymanager; here entitymanager not getting injected , null. can 1 please me wrong in code.
here configuration.
user.java
@entity @table(name="user") public class user { private int id; private string name; private string surname; @id @sequencegenerator(name="user_id_seq_gen", sequencename="user_id_gen_seq", allocationsize=1) @generatedvalue(strategy=generationtype.sequence, generator="user_id_seq_gen") @column(name="id", unique = true, nullable = false) public int getid() { return id; } public void setid(int id) { this.id = id; } @column(name="name", unique = true, nullable = false) public string getname() { return name; } public void setname(string name) { this.name = name; } @column(name="surname", unique = true, nullable = false) public string getsurname() { return surname; } public void setsurname(string surname) { this.surname = surname; } } persistence.xml
<?xml version="1.0" encoding="utf-8"?> <persistence 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" version="2.0"> <persistence-unit name="testunit" transaction-type="resource_local"> <provider>org.hibernate.ejb.hibernatepersistence</provider> <non-jta-data-source>java:/comp/env/testds</non-jta-data-source> <class>com.user.external.test.user</class> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.oracle10gdialect"/> <property name="hibernate.max_fetch_depth" value="6"/> <property name="hibernate.show_sql" value="false"/> <property name="hibernate.format_sql" value="true"/> </properties> </persistence-unit> </persistence> tomcat server.xml
<context docbase="usertools" path="/usertools" reloadable="true"> <resourcelink name="testds" global="testds" type="javax.sql.datasource"/> </context> testbean.java
@managedbean(name="testbean") @viewscoped public class testbean implements serializable{ @managedproperty("#{testservice}") private itestservice testservice; public void savedata(user user){ testservice.saveuser(user); } public void settestservice(itestservice testservice){ this.testservice = testservice; } public itestservice gettestservice(){ return testservice; } } testserviceimpl.java
@managedbean(name="testservice") @applicationscoped public class testserviceimpl implements itestservice { @managedproperty(value="#{testdao}") private itestdao testdao; public void saveuser(user user){ testdao.saveuser(user); } public void settestdao(itestdao testdao){ this.testdao = testdao; } public itestdao gettestdao(){ return testdao; } } testdao.java
@managedbean(name = "testdao") @applicationscoped public class testdao implements itestdao { @persistencecontext private entitymanager entitymanager; // null public void saveuser(user user){ entitymanager.persist(user); // getting null pointer here.... } public void setentitymanager(entitymanager entitymanager) { this.entitymanager = entitymanager; } public entitymanager getentitymanager() { return entitymanager; } } i have tried @persistencecontext name & unitname attributes. still not working. tried resource config in web.xml
<persistence-context-ref> <persistence-context-ref-name>testunit</persistence-context-ref-name> <persistence-unit-name>testunit</persistence-unit-name> </persistence-context-ref> <resource-ref> <description>db connection</description> <res-ref-name>testunit</res-ref-name> <res-type>javax.sql.datasource</res-type> <res-auth>container</res-auth> </resource-ref> still no luck. can 1 please me.
tomcat not java ee container, there limitations related container managed factories , dependency injection. among others, need manually create entitymanagerfactory , entitymanager.
hibernate documentation isn't clear on that, here's eclipselink one: tomcat/jpa tutorial. check "limitations jpa" section, applies hibernate.
Comments
Post a Comment