How to write complex QueryDSL queries with attribute maps
In this article, I’ll show you how to write queries to JPA entities with attribute maps (fields marked with @ElementCollection) using QueryDSL.
Note that this is not a guide on how to structure your entity model. In most cases, you don’t even need a set of arbitrary attributes in your entity. But when you do (due to different reasons — legacy data structures, additional flexibility etc.), then the queries can get really tricky.
Suppose we have the following User entity with a map of arbitrary attributes:
@Entity
public class User implements Serializable {
@Id
@GeneratedValue
private Long id;
@ElementCollection
@MapKeyColumn(name = "key")
@Column(name = "value")
private Map<String, String> attributes = new HashMap<>();
// getters and setters skipped
}
We create an entity and persist it:
Map<String, String> attributes = new HashMap<>();
attributes.put(“name”, “Mary”);
attributes.put(“birthDate”, “18–10–1983”);
attributes.put(“personalNumber”, “42”);
User user = new User();
user.setAttributes(attributes);
entityManager.persist(user);
We also have an entity PersonalNumber with the number field. This field should have the same value as in the “personalNumber” attribute of the User entity. The problem is that due to legacy reasons, the PersonalNumber.number field contains not exactly the same value, but a fixed-length value left-padded with zeroes:
@Entity
public class PersonalNumber implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column
private String number;
// getters and setters skipped
}
PersonalNumber personalNumber = new PersonalNumber();
personalNumber.setNumber("000042");
entityManager.persist(personalNumber);
Now we need to find an entity using a complex condition. We need to find a user named Mary, born in 1983, and also join it with the PersonalNumber table by the personalNumber attribute.
Here’s how to do it:
StringExpression name = CommonExpressions.constant("name");
StringExpression birthDate =
CommonExpressions.constant("birthDate");
StringExpressions personalNumber =
CommonExpressions.constant("personalNumber");
QUser USER = QUser.user;
QPersonalNumber PERSONAL_NUMBER = PersonalNumber.personalNumber;
List<User> matchedUsers = HibernateQuery<User>.from(
USER, PERSONAL_NUMBER)
.where(USER.attributes.contains(name, "Mary")
.and(USER.attributes.get(birthDate).substring(6).eq("1983"))
.and(StringExpressions.lpad(
USER.attributes.get(personalNumber), 6, '0')
.eq(PERSONAL_NUMBER.number))
.fetch();
First, we create a full join between the USER and the PERSONAL_NUMBER entities.
Then, using the User.attributes.contains() method, we add a condition to filter out only the User entities with a name attribute equal to “Mary”.
In the next condition, we use the USER.attributes.get() method to create a condition by the birthDate attribute. We use the substring() method to extract the year from this attribute and compare it to the expected value.
Finally, in the last condition, we use StringExpressions.lpad() method of QueryDSL to pad the value of the attribute personalNumber with zeroes. Then it becomes directly comparable to the value from the PersonalNumber entity, and we can join the entities by this condition.