gpt4 book ai didi

java - Hibernate查询、QueryException异常

转载 作者:行者123 更新时间:2023-11-29 07:09:03 25 4
gpt4 key购买 nike

我有一个疑问:

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

但我有异常(exception)。为什么?

[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;
}
}

最佳答案

您的实体没有任何名为 linked_id 的属性。不过,它有一个名为linkedId。因此,这就是您应该在 JPQL 查询中使用的内容。

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

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

关于java - Hibernate查询、QueryException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40532128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com