Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:36:00+00:00 2026-06-06T14:36:00+00:00

I am using JSF2 with Pure JPA2. But the problem is with entityManager, @PersistenceContext

  • 0

I am using JSF2 with Pure JPA2.
But the problem is with entityManager,

@PersistenceContext
private EntityManager entityManager;

Here entityManager is not getting injected and always null.
Can some one please help me what is wrong in my code.

Here is my 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; // is 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 with @PersistenceContext name & unitName attributes. But still it is not working. Also tried with 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 some one please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:36:01+00:00Added an answer on June 6, 2026 at 2:36 pm

    Tomcat is not a Java EE container, so there are limitations related to container managed factories and dependency injection. Among others, you need to manually create the EntityManagerFactory and the EntityManager.

    Hibernate documentation isn’t clear on that, so here’s the Eclipselink one: Tomcat/JPA tutorial. Check the "Limitations to JPA" section, this applies as good to Hibernate.

    See also:

    • Best practice to get EntityManagerFactory
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JSF2.0 and am building up a wizard. I have encountered a problem
I have using jsf2.0 in eclipse ide.I have a problem to store a image
We are using JSF2.0.2 in weblogic10.3 , we are handling FileNotFound or Page Not
I am using JSF2,primefaces2.1 (using netbeans 7.0.1 and glassfish3.1) the problem that i am
I'm using JSF 2.0 and it's new (actually old, but now incorporated in JSF)
I am using JSF2 on Facelets. I define an <ui:param> in a page: <ui:composition
This code, a test case for a custom component using JSF2 Just for the
I am using Primefaces and JSF2 . All my .xhtml files are dumped in
I'm using the new JSF2 <h:link> tag, with a nested <f:param> to link to
I'm developing a JSF2 application using Glassfish which contains some EJB's; is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.