Java MappingException:命名查询未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26084031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:49:21  来源:igfitidea点击:

MappingException: Named query not known

javahibernatejpahibernate-mappingnamed-query

提问by NoobEditor

Trying to learn Hibernate, i am trying to learn how to execute NamedQuriesbut evertime i am getting Exception in thread "main" org.hibernate.MappingException: Named query not known.Please help me out here

正在尝试学习 Hibernate,我正在尝试学习如何执行,NamedQuries但每次我都会遇到Exception in thread "main" org.hibernate.MappingException: Named query not known。请在这里帮助我

Error(only the message, not showing complete stack)

错误仅显示消息,未显示完整堆栈

Exception in thread "main" org.hibernate.MappingException: Named query not known: hibernate_tut_emp.Employee.FindCountOfNames
    at org.hibernate.internal.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:177)
    at org.hibernate.internal.SessionImpl.getNamedQuery(SessionImpl.java:1372)
    at hibernate_tut_emp.MyOps.main(MyOps.java:20)

Employee.java

雇员.java

package hibernate_tut_emp;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import javax.persistence.Table;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;



@Entity
@Table(name="Hib1")

@NamedQueries({
    @NamedQuery(name="GetDetailsByName" , query="select * from hib1 h where h.name=:name"),
    @NamedQuery(name="FindCountOfNames", query="select count(1) as cnt from hib1 h where h.name=:name")
})
public class Employee {
    private int id;
    private String name;

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

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

    @Column(name="name")
    public String getName() {
            return name;
    }

    public void setName(String empName) {
            this.name = empName;
    }


}

MyOps.java

MyOps.java

package hibernate_tut_emp;

import java.util.Scanner;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class MyOps {

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);
        System.out.print("Enter a name : ");
        String name = scnr.next();

        SessionFactory ses = HibernateUtil.getSessionFactory();
        Session session = ses.openSession();

        Query query = session.getNamedQuery("hibernate_tut_emp.Employee.FindCountOfNames");


        query.setString("name", name);
        int count = ((Integer)query.iterate().next()).intValue();
        System.out.println("count : "+count);
    }
}

employee.hbm.xml

员工档案.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="hibernate_tut_emp.Employee" table="hib1">
        <id name="id" type="int" column="id">
            <generator class="native" />
        </id>
        <property name="name" type="string" column="name" />
    </class>
</hibernate-mapping>

hibernate.cfg.xml

休眠文件.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
         <property name="connection.username">root</property>
         <property name="connection.password">mayank</property>

        <!-- JDBC connection pool (use the built-in) -->
         <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
          <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
         <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
         <property name="hbm2ddl.auto">update</property>

      <mapping class="hibernate_tut_emp.Employee" />
      <mapping resource="employee.hbm.xml"/>

     </session-factory>
 </hibernate-configuration>

I googled out everything possible but i think i am overlooking some basic stuff.Any indication in right direction is appreciated! :)

我用谷歌搜索了所有可能的东西,但我想我忽略了一些基本的东西。任何正确方向的指示都值得赞赏!:)

One thing i considered is that if i have defined in class file NamedQueries, i do not need to mention it in xmlfile.Please correct me if i am wrong!

我考虑的一件事是,如果我在类文件中定义了NamedQueries,我不需要在xml文件中提及它。如果我错了,请纠正我!

采纳答案by Vlad Mihalcea

There are several issues here:

这里有几个问题:

  1. Your named queries should use entities not tables. If you want native queries you should use NamedNativeQueryinstead.

  2. You don't need to supply the entity name when fetching the query.

    Change this:

    Query query = session.getNamedQuery("hibernate_tut_emp.Employee.FindCountOfNames");
    

    to:

    Query query = session.getNamedQuery("FindCountOfNames");
    
  1. 您的命名查询应该使用实体而不是表。如果您需要本机查询,则应改用NamedNativeQuery

  2. 获取查询时无需提供实体名称。

    改变这个:

    Query query = session.getNamedQuery("hibernate_tut_emp.Employee.FindCountOfNames");
    

    到:

    Query query = session.getNamedQuery("FindCountOfNames");