0

我正在使用带有休眠功能的 JPA 类因此,我正在使用 persistence.xml 文件进行数据库连接

我想从数据库中删除多行因此我创建了以下代码

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)

我附在 Node.java 文件下面。它包含所有注释。我需要创建映射文件吗?

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
}

下面是 CompositeKeyClass NodePrimaryKey

@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
}

持久性.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>
4

1 回答 1

0

错误很明显:

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

编辑:
您使用的是@Table名称(NODE)而不是@Entity名称Node(默认是简单的类名);HQL 适用于实体,而不是物理表名。

利用:

Query query = em.createQuery("delete from Node o " + "where o.BUILDING_NAME = :name and o.FLOOR_NO= :no");
于 2013-08-26T14:37:21.643 回答