Issue
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? 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]
Solution
In following IdmTenantBoardPK
is type of the embedded id and name of the property is id
:
@EmbeddedId
private IdmTenantBoardPK id;
That's why o.IdmTenantBoardPK.boardId
cannot be used in queries. Instead of it o.id.boardId
should be used.
Answered By - Mikko Maunu