使用EclipseLink和Querydsl的自定义数据类型

时间:2013-09-26 01:35:49

标签: java jpa eclipselink querydsl

我已经创建了一个自定义数据类型,用于存储有效和规范化的电子邮件地址:

public class Email implements Serializable {

    private final String value;

    public Email(String emailAddress) {
        this.value = normalize(emailAddress);
        if (!isValid(value)) {
            throw new IllegalArgumentException("Email address format is not valid: " + value);
        }
    }
    ...
}

它的通讯员JPA 2.1转换器用于自动存储和从数据库中检索:

@Converter(autoApply = true)
public class EmailConverter implements AttributeConverter<Email, String> {

    @Override
    public String convertToDatabaseColumn(Email email) {
        return email == null ? null : email.toString();
    }

    @Override
    public Email convertToEntityAttribute(String email) {
        return email == null ? null : new Email(email);
    }

}

现在,我可以将它用作人物实体的属性:

public class Person extends BaseEntity {

    private String name;

    private LocalDate birthDate;

    @QueryType(PropertyType.STRING)
    private Email email;

    ...
}

这完美无缺。但是当我尝试通过使用Querydsl开始他们的电子邮件地址找到所有人时,我遇到了问题。我用@QueryType作为字符串注释了电子邮件属性。通过这种方式,Querydsl元模型被创建为我可以进行查询(使用.startsWith()),如下所示:

private static final QPerson person = QPerson.person;

public List<Person> getEmailStartWith(String pattern) {
    pattern = Email.normalize(pattern);
    return new JPAQuery(getEntityManager())
        .from(person)
        .where(person.email.startsWith(pattern))
        .orderBy(person.email.asc())
        .list(person);
}

但是当我运行它时我得到了这个例外:

java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter 1 with expected type of class xxx.Email from query string select person
from Person person
where person.email like ?1 escape '!'
order by person.email asc.
at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:932) ~[eclipselink-2.5.0.jar:2.5.0.v20130507-3faac2b]
at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:906) ~[eclipselink-2.5.0.jar:2.5.0.v20130507-3faac2b]
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:469) ~[eclipselink-2.5.0.jar:2.5.0.v20130507-3faac2b]
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:1) ~[eclipselink-2.5.0.jar:2.5.0.v20130507-3faac2b]
at com.mysema.query.jpa.impl.JPAUtil.setConstants(JPAUtil.java:44) ~[querydsl-jpa-3.2.1.jar:na]
at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:130) ~[querydsl-jpa-3.2.1.jar:na]
at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:97) ~[querydsl-jpa-3.2.1.jar:na]
at com.mysema.query.jpa.impl.AbstractJPAQuery.list(AbstractJPAQuery.java:240) ~[querydsl-jpa-3.2.1.jar:na]
...

我可以使用JPQL获得正确的结果(以及一个不合理的技巧,concat):

public List<Person> getEmailStartWith(String pattern) {
    pattern = Email.normalize(pattern);
    return getEntityManager()
        .createQuery("select p from Person p where p.email like concat(?1, '%')", Person.class)
        .setParameter(1, pattern)
        .getResultList();
}

但我当然更喜欢Querydsl的类型安全。是否可以使用此库创建此查询?

1 个答案:

答案 0 :(得分:1)

根据 Querydsl 创建者Timo Westkämper,此issue EclipseLink 更相关。