[英]How to select and order an EmbeddedID in JPA
I have one table with a composite primary key:我有一个带有复合主键的表:
public class IdmTenantBoard implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private IdmTenantBoardPK id;
@Column(name="board_display_name")
private String boardDisplayName;
@Column(name="board_name")
private String boardName;
The embedded class looks like this:嵌入的类如下所示:
public class IdmTenantBoardPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="board_id")
private int boardId;
@Column(name="tenant_id")
private String tenantId;
The method I am using to call query is as follows:我用来调用查询的方法如下:
public String getMaxBoardId(){
TypedQuery<String> query = entityManager.createQuery("select o from IdmTenantBoard o order by o.IdmTenantBoardPK.boardId desc",String.class);
query.setMaxResults(1);
List<String> lResults = query.getResultList();
String maxBoardId=null;
if((null!=lResults)&&(!lResults.isEmpty())){
maxBoardId=lResults.get(0);
}
return maxBoardId;
}
What do I need to get the boardId
sorted in Descending order?我需要什么才能按降序对
boardId
进行排序? I am getting the following error:我收到以下错误:
Caused by: org.hibernate.QueryException: could not resolve property: IdmTenantBoardPK of: com.newrubric.idm.common.entities.IdmTenantBoard [select o from com.newrubric.idm.common.entities.IdmTenantBoard o order by o.IdmTenantBoardPK.boardId desc]
引起:org.hibernate.QueryException:无法解析属性:IdmTenantBoardPK 的:com.newrubric.idm.common.entities.IdmTenantBoard [从 com.newrubric.idm.common.entities.IdmTenantBoard 选择 o 由 o.IdmTenantBoardPK 订购。 boardId 描述]
In following IdmTenantBoardPK
is type of the embedded id and name of the property is id
:在下面的
IdmTenantBoardPK
是嵌入 id 的类型,属性的名称是id
:
@EmbeddedId
private IdmTenantBoardPK id;
That's why o.IdmTenantBoardPK.boardId
cannot be used in queries.这就是不能在查询中使用
o.IdmTenantBoardPK.boardId
的原因。 Instead of it o.id.boardId
should be used.应该使用
o.id.boardId
而不是它。
这里 'id' 是属性名称,因此使用o.id.boardId
而不是o.IdmTenantBoardPK.boardId
。
Using '.'使用“.” operator we can specify sorting functionality on embeddedId fields.
运算符,我们可以在 EmbeddedId 字段上指定排序功能。
@Embeddable
public class ProjectId implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "PROJECT_NAME")
private String projectName;
@Column(name = "ORGANIZATION")
private String organization;
......
......
}
@Entity
@Table(name = "projects")
public class Project {
@EmbeddedId
private ProjectId projectId;
@Column(name = "STARTED_TIME")
private Timestamp startedTime;
@Column(name = "ACTIVE")
private String active;
@Column(name = "DESCRIPTION")
private String description;
......
......
}
As you see above snippet ProjectId is a composite key for the entity Project.正如您在上面看到的片段 ProjectId 是实体项目的组合键。 I can specify sorting on organization field like below.
我可以在组织字段上指定排序,如下所示。
Sort sort = new Sort(Direction.ASC, Arrays.asList("projectId.organization"));
I had the same issue.我有同样的问题。 Try this, it works like a charm.
试试这个,它就像一个魅力。
Add this to your actual code将此添加到您的实际代码中
Sort sort = Sort.by(Sort.Direction.DESC,"projectId.organization");
Add this method to the Repository将此方法添加到存储库
List<AccessoriesFile> findByorganization(String organization, Sort sort);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.