当前位置: 首页 > 知识库问答 >
问题:

在Hibernate中使用JPQL(而不是HQL)

蒋胡非
2023-03-14
EntityManager em = EntityManagerFactoryClass.getEMF().createEntityManager();
try
{
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();
    Query query = em.createQuery("delete from NODE o " 
        + "where o.BUILDING_NAME = :name and o.FLOOR_NO= :no");
    query.setParameter("name", buildingName);
    query.setParameter("no", floorNo);
    int deleted = query.executeUpdate();
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: NODE is not mapped [delete from NODE o where o.BUILDING_NAME = :name and o.FLOOR_NO= :no]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1374)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:294)
at com.iitb.gise.evacuation.service.UploadNodePathService.saveNodeData(Unknown Source)
at com.iitb.gise.evacuation.pojo.DataObject.saveNodes(Unknown Source)
at com.iitb.gise.evacuation.bean.ImageRetriveBean.saveData(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at org.apache.myfaces.view.facelets.el.ContextAwareTagMethodExpression.invoke(ContextAwareTagMethodExpression.java:96)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
at javax.faces.component.UICommand.broadcast(UICommand.java:120)
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:1013)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:284)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1302)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:745)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:38)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import com.vividsolutions.jts.geom.Point;

@SuppressWarnings("serial")
@Entity
@Table(name="NODE")
public class Node implements Serializable{

@EmbeddedId
NodePrimaryKey nodePrimaryKey;
@Column(name = "NODE_NAME")
String nodeName;
@Column(name = "NODE_CAPACITY")
int nodeCapacity;

@Type(type="org.hibernate.spatial.GeometryType")
@Column(name="NODE_LOCATION")
Point location;
@Column(name = "NODE_TYPE")
String nodeType;            //Source or Destination

//getters and setters
}
@Embeddable
public class NodePrimaryKey implements Serializable {

private static final long serialVersionUID = 1L;
@Column(name="BUILDING_NAME")
String buildingName;
@Column(name="FLOOR_NO")
int floorNo;
@Column(name="NODE_ID")
String nodeId;
    //getters and setters
}

persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“>org.hibernate.ejb.hibernatePersistence

        <class>com.iitb.gise.evacuation.entity.Node</class>
        <class>com.iitb.gise.evacuation.entity.NodePrimaryKey</class>
    <properties>

        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/TEST" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <property name="hibernate.connection.username" value="postgres" />
        <property name="hibernate.connection.password" value="*****" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />

    </properties>
</persistence-unit>
</persistence>

共有1个答案

贺君浩
2023-03-14

错误很明显:

org.hibernate.hql.internal.ast.QuerySyntaxException: NODE is not mapped

编辑:
您使用的是@table名称(节点),而不是@entity名称(节点)(默认值是简单类名);HQL使用实体,而不是物理表名。

使用:

Query query = em.createQuery("delete from Node o " + "where o.BUILDING_NAME = :name and o.FLOOR_NO= :no");
 类似资料:
  • 问题内容: 我的实体当前包含java Date属性。我开始非常频繁地将Joda Time用于日期处理和计算。这意味着我一直必须将Dates转换为Joda DateTime对象,然后再次转换。 所以我想知道,有什么理由不应该仅更改我的实体来存储Joda DateTime对象而不是Java Date对象吗? 请注意,这些实体通过Hibernate保留。我找到了jodatime-hibernate项目,

  • 问题内容: 通常,我使用Hibernate的 @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 来缓存@Entity类,并且效果很好。 在JPA2中,还有另一个@Cacheable批注,该批注似乎与Hibernate的@Cache相同。为了使我的实体类独立于hibernate的包,我想尝试一下。但是我无法使其工作。每次简单的

  • 是否可以在Android Studio中为设备显示?我正在编写一个使用USB端口的应用程序,我无法将其用于。 目前,我正在使用以下指令查看命令提示符中的logcat,但如果将其集成到AS:http://developer.android.com/tools/help/adb.html#wireless

  • 在中有一些使用SQL over Spark Streaming的示例。但是如果我想在中使用SQL: 我犯了这样的错误: [error]/users/raochenlin/downloads/spark-1.2.0-bin-hadoop2.4/logstash/src/main/scala/logstash.scala:52:没有方法转换的类型参数:(transformfunc:org.apache

  • 问题内容: 我目前正在使用Spring 3.1和Hibernate 4通过以下方式扫描DAO和服务的软件包:是否可以对标记的类执行相同的操作,而不是使用属性和文件? 问题答案: 将扫描模型包中的所有内容。我使用cfg.xml包含show_sql和hb2ddl.auto之类的设置。

  • 我想要的是Hibernate生成1条语句删除所有子级的方法,而不是每个子级1条语句。 如果实体a和B之间存在关系,其中a是父对象,B可以是多个子对象。如果我在实体关系上使用注释,如OrphanRemoving或OnCascade delete,那么当我删除父对象时,自动生成的sql会显示 我想让Hibernate做的是生成SQL 是否有一个注释或设置我可以用来轻松做到这一点? 我觉得我不是第一个遇