gpt4 book ai didi

java - 映射异常 : Named query not known

转载 作者:行者123 更新时间:2023-12-04 14:58:08 26 4
gpt4 key购买 nike

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

错误 (只有消息,没有显示完整的堆栈)

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)

员工.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
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);
}
}

员工.hbm.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
<?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>

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

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

最佳答案

这里有几个问题:

  • 您的命名查询应该使用实体而不是表。如果你想要原生查询,你应该使用 NamedNativeQuery反而。
  • 获取查询时无需提供实体名称。

    改变这个:
    Query query = session.getNamedQuery("hibernate_tut_emp.Employee.FindCountOfNames");

    到:
    Query query = session.getNamedQuery("FindCountOfNames");
  • 关于java - 映射异常 : Named query not known,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26084031/

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