【问题标题】:Find an entity which use a class Id查找使用类 ID 的实体
【发布时间】:2018-05-21 07:04:54
【问题描述】:

要从具有主键的实体中查找对象,我们使用em.find(Person.class, <Id>)

我正在使用 JPA EclipseLink,并且我有一个具有复合主键 (@classId) 的 Person 实体,

Person 实体:

@Entity
    @IdClass(PersonId.class)
    public class Person {

@Id
private int id;

@Id
private String name;

public String getName() {
    return name;
}

// getters & setters
}

和 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
}

如何使用 em.find 获取 Person 对象?

【问题讨论】:

    标签: jpa eclipselink jpql entitymanager


    【解决方案1】:

    我找到了解决方案:

        PersonId carId = new PersonId(33, "Jhon");
    
        Person persistedPerson = em.find(Person.class, carId);
    
        System.out.println(persistedPerson.getID() + " - " + persistedPerson.getName());
    

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      相关资源
      最近更新 更多