Home find an entity using a class Id
Reply: 1

find an entity using a class Id

M.A.Bell Published in 2017-12-07 08:22:16Z

To find an object from entity with primary key we use em.find(Person.class, <Id>).

I'm using JPA EclipseLink and I have a Person entity wich has a composite primary key(@classId),

the Person entity:

    public class Person {

private int id;

private String name;

public String getName() {
    return name;

// getters & setters

and PersonID:

public class PersonId implements Serializable {

private static final long idVersionUID = 343L;

private int id;
private String name;

// must have a default construcot
public PersonId() {


public PersonId(int id, String name) {
    this.id = id;
    this.name = name;

//getters & setters
//hachCode & equals

How to use em.find to get a specified Person object?

M.A.Bell Reply to 2017-12-07 08:22:16Z
PersonId carId = new PersonId(33, "Jhon");

Person persistedPerson = em.find(Person.class, carId);

System.out.println(persistedPerson.getID() + " - " + persistedPerson.getName());
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301538 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO