通过HQL(Hibernate)选择类型enumarated集合的Entity属性

时间:2018-06-18 14:05:08

标签: hibernate hql

我们正在使用java + hibernate设置,我的实体(和枚举)看起来像这样:

@Entity
@Table(indexes = {@javax.persistence.Index(columnList = "sendDate", name = "foo_bar_idx"),...})
@EntityDomain(EntityType.Message)
public class Message extends AuditedClass {

    /**
     * The Types.
     */
    @Enumerated(EnumType.STRING)
    @ElementCollection
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    List<MessageType> types;


    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
    LocalDateTime sendDate;

    //more attributes
    ...
    //getters and setters
    ...
}

public enum MessageType{
    VALUE_1,
    VALUE_2;
}

我正在getCurrentSession().createQuery(queryString);跟踪queryString

select m.sendDate, m.types from Message m

但是我收到了错误:

org.hibernate.QueryException: not an entity [select m.sendDate, m.types from mydomain.message.Message m]

我做错了什么?还有其他想法吗? Hibernate 5.0.11

更新

这里要求的是查询代码的本质:

//imports
...
@Component("messageService")
@Transactional
public class MessageServiceImpl extends GenericHibernateServiceImpl implements MessageService {
    //@Autewired services
    ...
    //    other methods
    ...

    @Override
    public List<Foo> getFoos(/*inconsequential arguments*/) {

        List<Foo> list = new Vector<Foo>();
        try {
            Query query = getCurrentSession().createQuery("select m.sendDate, m.types from Message m");
            //execution does not reach here, error is thrown on the row above
        }
        catch (Throwable t) {
            //log and stuff
        }

        return list;
    }

    //    more methods
    ...
}

这是弹簧控制器自动装配和使用的方法的一部分。

更新2 - 按要求进行堆栈跟踪:

Exception: not an entity [select m.sendDate, m.types from cz.trask.edoceo.domain.message.Message m]. StackTrace: org.hibernate.QueryException: not an entity [select m.sendDate, m.types from cz.trask.edoceo.domain.message.Message m]
    at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894)
    at cz.trask.edoceo.service.impl.MessageServiceImpl.getFoos(MessageServiceImpl.java:388)
    //over 100 traces
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.QueryException: not an entity
    at org.hibernate.hql.internal.ast.tree.FromElementType.renderIdentifierSelect(FromElementType.java:174)
    at org.hibernate.hql.internal.ast.tree.FromElement.renderIdentifierSelect(FromElement.java:208)
    at org.hibernate.hql.internal.ast.tree.SelectClause.renderNonScalarIdentifiers(SelectClause.java:465)
    at org.hibernate.hql.internal.ast.tree.SelectClause.renderNonScalarSelects(SelectClause.java:421)
    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:244)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:920)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:688)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:673)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:309)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:257)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
    ... 122 more
, user=sysadmin, entityType=null, entityId=null

0 个答案:

没有答案