gpt4 book ai didi

java - Hibernate - 命名查询未知

转载 作者:行者123 更新时间:2023-12-02 05:16:25 27 4
gpt4 key购买 nike

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

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

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();

这会返回一个错误:

Exception in thread "main" org.hibernate.MappingException: Named query not known: User.findByHash at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93) at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1407) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at 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) at wroproject.UserConnection._getUserByHash(UserConnection.java:44) at wroproject.UserConnection.(UserConnection.java:26) at 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>

我应该做什么?

最佳答案

令我感到奇怪的是,您同时使用 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 时注册了哪些实体。

关于java - Hibernate - 命名查询未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915409/

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