0

我有两个实体,它们之间有关系。

public class Client implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Integer id;

@NotNull
@Size(min = 3, max = 25)
private String firstName;

@NotNull
@Size(min = 3, max = 25)
private String lastName;

private String login;

private String password;

@OneToMany(mappedBy = "client")
private List<Project> projects;
}

public class Project implements Serializable {
private static final long serialVersionUID = 4762714047114442539L;

@Id
@GeneratedValue
private Integer id;

private String name;

@Temporal(TemporalType.TIMESTAMP)
private Date startDate;

@ManyToOne
@JoinColumn
private Client client;
}

我想使用 jpametamodel 和 Criteria API 进行查询。像这样:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Project> q = cb.createQuery(Project.class);
Root<Project> projects = q.from(Project.class);
q.where(cb.equal(projects.get(Project_.client), clientId));

我的问题是我不知道如何访问此字符串中客户端的“id”属性:

q.where(cb.equal(projects.get(Project_.client), clientId));

我想得到类似的东西

q.where(cb.equal(projects.get("client.id"), clientId));

但是使用 jpametamodel。有可能的?:)

4

1 回答 1

3

Tried something like this?

projects.get(Project_.client).get(Client_.id);
于 2013-01-21T11:46:02.633 回答