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

  • Home
  • SEARCH
  • 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 8999241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:09:14+00:00 2026-06-16T00:09:14+00:00

When I use a Idclass in JPA for a entity with a composite key

  • 0

When I use a Idclass in JPA for a entity with a composite key and I write a remove method like :

private static void removeEmployee(Employee employee) {
    EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("JPA_excer_3b");
    EntityManager em = emf.createEntityManager();
    try {
        em.getTransaction().begin();
        Employee anemployee = em.find(Employee.class, employee.getId());
        em.remove(anemployee);
        em.getTransaction().commit();
    } catch (Exception e) {
        logging.error("This error is added a removing a employee :"
                + " :" + e);
    } finally {
        // Close all the connections:
        em.close();
        emf.close();
    }
}

I get the error :

ERROR Logger:22 - This error is added a removing a employee :java.lang.IllegalArgumentException: Provided id of the wrong type for class be.leerstad.JPA.entities.Employee. Expected: class be.leerstad.JPA.entities.EmployeePK, got class java.lang.Integer

When I change the code to :

private static void removeEmployee(Employee employee) {
    EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("JPA_excer_3b");
    EntityManager em = emf.createEntityManager();
    try {
        **EmployeePK pk = new EmployeePK(employee.getId(),employee.getEmail());**
        em.getTransaction().begin();
        Employee anemployee = em.find(Employee.class, **pk**);
        em.remove(anemployee);
        em.getTransaction().commit();
    } catch (Exception e) {
        logging.error("This error is added a removing a employee :"
                + " :" + e);
    } finally {
        // Close all the connections:
        em.close();
        emf.close();
    }
}

I don’t get any errors but I wonder if this is the right way to do this?

  • 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-16T00:09:15+00:00Added an answer on June 16, 2026 at 12:09 am

    Yes, it’s the right way to do it. The EntityManager.find() method expects an instance of the ID class of the entity. Not some random attribute of the entity.

    Note that the error isn’t caused by the remove() method, but by the find() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use jquery to remove some tags inside mainclass, so that
If you have a IdClass like public class EmployeePK implements Serializable { private String
I'm trying to add a composite primary key to a class and having a
use javascript or other method ? have any recommendation?
I was wondering if it was possible to use the find method to order
I have several Entity classes. Now I want to use @PostLoad in every entity.
Use the grow() method to modify a rectangle to be twice as large. I'm
I would like to use the middle mouse button to drag an image in
i've got two Entities with combinded primarky keys: @Entity @IdClass(APK.class) public class A {
Using JPA, can we define an enum as id of an entity? I've tried

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.