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 7495959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:24:34+00:00 2026-05-29T18:24:34+00:00

I am doing a small project using JPA. I need to insert the employee

  • 0

I am doing a small project using JPA. I need to insert the employee object.
For that when I use the annotated entity manager I got the NullPointer exception.
But when I use the Normal EntityManager without using the annotation it is working fine.
Do I need to configure somewhere else other than persistence.xml to work this examle fine?

Please see the code below.

public class EmployeeDao implements IEmployeeDao{       

     @PersistenceContext(unitName = "timesheet")
    private EntityManager entityManager ;

    @Override
    public boolean createEmployee(IEmployee employee) { 

        this.entityManager.persist(employee);       

        return true;
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="timesheet" transaction-type="RESOURCE_LOCAL">       
        <class>com.timesheet.model.Employee</class>     
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost:3306/timesheet" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />

            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
        </properties>

    </persistence-unit>
</persistence>
  • 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-05-29T18:24:41+00:00Added an answer on May 29, 2026 at 6:24 pm

    Injection of resources (in your case via use of @PersistenceContext) works only in container managed classes (like EJBs and Servlets). This explained with more details for example in Java EE specification v6, EE5.2.5.

    What you can do:

    • Modify your class so that it is managed class
    • move injection of resources to managed class and pass it to
      EmployeeDao,
    • use JNDI lookup as before
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a small project in Django by using Python. In that, I am
i am doing a small project in django by using python. In that project,
i doing a small project in django by using python. In that admin can
I have a small project I am doing in Python using web.py. It's a
I have a small- to medium-size project that I am doing for my software
I have a client that I am doing a very small project for. His
I'm doing a small test project before I use System.Threading.Timer in a Windows Service
I am doing a small reporting project that needs to read a lot of
I've got a small Java program that I'm developing for a project, which pulls
i'm working on a small project that is supposed to allow basic searches of

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.