JPA键入的Query在构造函数中传递Map中的值

时间:2014-02-06 05:33:22

标签: java performance hibernate java-ee jpa

在JPQL TypedQuery中,我们可以选择值并直接在其他类的construtor中传递值。这工作正常。我希望在键值配对映射中选择这些记录,以便我可以在变量参数construtor中传递整个映射,并从map中的类属性名称中分配它的值(通过从其属性名称获取值)。有没有办法实现这个目标?

这是我的问题:

hqlQuery.append("select new mypackage.Myclass( new map('employeeCode',he.employeeCode) , new map('resignationID',he.employeeId))");

MyClass{
    private String employeeCode;
    private Integer employeeId;
    public MyClass(Map<String, Object>... maps) //Variable map
    {
        super();
        for (Map<String, Object> map : maps) {
            if (map.containsKey("employeeId")) {
                this.employeeId= (Integer) map.get("employeeId");
            }
            if (map.containsKey("employeeCode")) {
                this.employeeCode = (String) map.get("employeeCode");
            }
        }
    }

我想这样做,以便在类的属性太多的情况下我不必记住construtor参数顺序。

当我创建查询时,我收到此错误:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: new near line 1, column 70 [select new com.talentpact.businessobject.resignation.ResignationTO(  new map('resignationID',resignation.resignationId)  ) from com.talentpact.model.HrEmployeeResignation resignation   join  resignation.hrEmployee he   left join  resignation.expectedRelievingDateId where resignation.resignationId=:resignationId ]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
    at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:276)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:295)
    ... 165 more

0 个答案:

没有答案