Hibernate - 命名查询未知

时间:2014-11-13 17:59:48

标签: java hibernate

我有一个奇怪的问题。我在互联网上搜索了许多解决方案,但没有人帮忙。

我有这个简单的方法从我的数据库中获取信息:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();

session.beginTransaction();

Query query = session.getNamedQuery("User.findByHash");
query.setString("hash", hash);
query.setDate("hash", new Date());
List<?> list = query.list();
User user = (User) list.get(0);

session.getTransaction().commit();

这会给我一个错误:

  

线程“main”中的异常org.hibernate.MappingException:命名查询   不知道:User.findByHash at   org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)     在   org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1407)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:483)at   org.hibernate.context.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)     at com.sun.proxy。$ Proxy0.getNamedQuery(Unknown Source)   在wroproject.UserConnection._getUserByHash(UserConnection.java:44)     在wroproject.UserConnection。(UserConnection.java:26)     在wroproject.Main.main(Main.java:19)

User.java文件的顶部如下所示:

@Entity
@Table(name = "user", catalog = "wroproject", schema = "public")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
    @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"),
    @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
    @NamedQuery(name = "User.findByHash", query = "SELECT \"user\".* FROM \"user\" LEFT JOIN session on \"user\".id = session.user_id WHERE session.hash = :hash AND date_time_terminate < :date")
})
public class User implements Serializable {...}

我的fibernate.cfg.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/wroproject</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">postgres</property>

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

persistence.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="WroProjectPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>wrocproject.db.User</class>
    <class>wrocproject.db.Email</class>
    <class>wrocproject.db.Session</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/wroproject"/>
      <property name="javax.persistence.jdbc.password" value="postgres"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.user" value="postgres"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
  </persistence-unit>
</persistence>

我该怎么办?

2 个答案:

答案 0 :(得分:1)

我很奇怪您正在使用hibernate.cfg.xml和persistence.xml文件来配置hibernate。如果您使用的是hibernate-core,那么使用hibernate.cfg.xml,如果您使用的是hibernate-entitymanager,请使用persistence.xml。

由于您正在使用SessionFactory,因此可能会忽略您的persistence.xml以进行配置。由于persistence.xml是您指定实体的位置,因此我怀疑您的实体都没有被映射。您可以通过尝试加载实体而不使用存储过程来测试它:

session.get(User.class, id);

此外,您可以打开日志记录,并在创建SessionFactory时查看正在注册的实体。

答案 1 :(得分:0)

在查询中是这个参数:

hash AND date_time_terminate

更改:

Query query = session.getNamedQuery("User.findByHash");
query.setString("hash", hash);
query.setDate("hash", new Date());

人:

Query query = session.getNamedQuery("User.findByHash");
query.setString("hash", hash);
query.setDate("date_time_terminate", new Date());