Hibernate查询,QueryException异常

时间:2016-11-10 16:10:53

标签: java mysql sql hibernate

我有一个查询:

final Query query2 = session.createQuery("FROM CallDetailHistoriesEntity WHERE unique_id = :id OR unique_id IN (SELECT linked_id FROM CallDetailHistoriesEntity WHERE unique_id = :id)");

但我有例外。为什么呢?

[nioEventLoopGroup-5-5] ERROR com.corundumstudio.socketio.listener.DefaultExceptionListener - org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'linked_id' {originalText=linked_id}
 [FROM hibernate.entity.CallDetailHistoriesEntity WHERE unique_id = :id OR unique_id IN (SELECT linked_id FROM hibernate.entity.CallDetailHistoriesEntity WHERE unique_id = :id)]
java.lang.IllegalArgumentException: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'linked_id' {originalText=linked_id}
 [FROM hibernate.entity.CallDetailHistoriesEntity WHERE unique_id = :id OR unique_id IN (SELECT linked_id FROM hibernate.entity.CallDetailHistoriesEntity WHERE unique_id = :id)]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:131)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:102)
    at socketio.listeners.CallHistoryListener.onData(CallHistoryListener.java:27)
    at socketio.listeners.CallHistoryListener.onData(CallHistoryListener.java:22)
    at com.corundumstudio.socketio.namespace.Namespace.onEvent(Namespace.java:134)
    at com.corundumstudio.socketio.handler.PacketListener.onPacket(PacketListener.java:103)
    at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:92)
    at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:36)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:187)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
    at com.corundumstudio.socketio.transport.WebSocketTransport.channelRead(WebSocketTransport.java:94)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at com.corundumstudio.socketio.transport.PollingTransport.channelRead(PollingTransport.java:109)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at com.corundumstudio.socketio.handler.AuthorizeHandler.channelRead(AuthorizeHandler.java:132)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:108)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:182)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'linked_id' {originalText=linked_id}
 [FROM hibernate.entity.CallDetailHistoriesEntity WHERE unique_id = :id OR unique_id IN (SELECT linked_id FROM hibernate.entity.CallDetailHistoriesEntity WHERE unique_id = :id)]
    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:77)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:545)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:654)
    ... 41 more
Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'linked_id' {originalText=linked_id}

    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:160)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:991)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:759)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:675)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.collectionFunctionOrSubselect(HqlSqlBaseWalker.java:4918)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.inRhs(HqlSqlBaseWalker.java:4819)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4486)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2134)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2087)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:813)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:607)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
    ... 47 more

///////////////////////////////////// 我的Hibernate实体: ///////////////////////////////////// ///////////////////////////////////// /////////////////////////////////////

@Entity
@DynamicUpdate
@DynamicInsert
@Table(name = "call_detail_histories", schema = "mydb", catalog = "")
public class CallDetailHistoriesEntity {
    private int id;
    private String unique_id;
    private String source_caller_id;
    private String dest_caller_id;
    private Timestamp start_time;
    private Timestamp eventTime;
    private String current_status;
    private String linkedId;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "unique_id")
    public String getUnique_id() {
        return unique_id;
    }

    public void setUnique_id(String unique_id) {
        this.unique_id = unique_id;
    }

    @Basic
    @Column(name = "source_caller_id")
    public String getSource_caller_id() {
        return source_caller_id;
    }

    public void setSource_caller_id(String source_caller_id) {
        this.source_caller_id = source_caller_id;
    }

    @Basic
    @Column(name = "dest_caller_id")
    public String getDest_caller_id() {
        return dest_caller_id;
    }

    public void setDest_caller_id(String dest_caller_id) {
        this.dest_caller_id = dest_caller_id;
    }

    @Basic
    @Column(name = "start_time")
    public Timestamp getStart_time() {
        return start_time;
    }

    public void setStart_time(Timestamp start_time) {
        this.start_time = start_time;
    }

    @Basic
    @Column(name = "event_time", insertable = false)
    public Timestamp getEventTime() {
        return eventTime;
    }

    public void setEventTime(Timestamp eventTime) {
        this.eventTime = eventTime;
    }

    @Basic
    @Column(name = "status")
    public String getCurrent_status() {
        return current_status;
    }

    public void setCurrent_status(String current_status) {
        this.current_status = current_status;
    }

    @Basic
    @Column(name = "linked_id")
    public String getLinkedId() {
        return linkedId;
    }

    public void setLinkedId(String linkedId) {
        this.linkedId = linkedId;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CallDetailHistoriesEntity that = (CallDetailHistoriesEntity) o;

        if (id != that.id) return false;
        if (unique_id != null ? !unique_id.equals(that.unique_id) : that.unique_id != null) return false;
        if (source_caller_id != null ? !source_caller_id.equals(that.source_caller_id) : that.source_caller_id != null)
            return false;
        if (dest_caller_id != null ? !dest_caller_id.equals(that.dest_caller_id) : that.dest_caller_id != null) return false;
        if (start_time != null ? !start_time.equals(that.start_time) : that.start_time != null) return false;
        if (eventTime != null ? !eventTime.equals(that.eventTime) : that.eventTime != null) return false;
        if (current_status != null ? !current_status.equals(that.current_status) : that.current_status != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (unique_id != null ? unique_id.hashCode() : 0);
        result = 31 * result + (source_caller_id != null ? source_caller_id.hashCode() : 0);
        result = 31 * result + (dest_caller_id != null ? dest_caller_id.hashCode() : 0);
        result = 31 * result + (start_time != null ? start_time.hashCode() : 0);
        result = 31 * result + (eventTime != null ? eventTime.hashCode() : 0);
        result = 31 * result + (current_status != null ? current_status.hashCode() : 0);
        return result;
    }
}

1 个答案:

答案 0 :(得分:2)

您的实体没有任何名为linked_id的媒体资源。但它有一个名为linkedId的。因此,您应该在JPQL查询中使用它。

我还强烈建议坚持Java命名约定:变量和方法在Java中永远不会有下划线,并使用camelCase。因此,我还将unique_id重命名为uniqueId,将source_caller_id重命名为sourceCallerId等。当然,应相应更改getter和查询。

下一步是要认识到JPA / Hibernate的重点是拥有一个实体图,使用关联互连。因此,您应该在实体中没有类型为String的linkedId属性,而是与另一个实体的ManyToOne或OneToOne关联。这就是allwos导航到代码中的实体图表,并在查询中使用联接的内容。