gpt4 book ai didi

java - Hibernate 找不到刚刚创建的表的类映射

转载 作者:行者123 更新时间:2023-12-02 06:35:11 24 4
gpt4 key购买 nike

我让 hibernate 为我创建数据库架构。它能够很好地创建表,但命名查询无法编译,因为它声称对象未映射。

整个应用程序是注释和配置驱动的:没有 .xml 文件。

以下是相关配置部分:

properties = new Properties();
properties.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
properties.setProperty("hibernate.connection.url", con);
properties.setProperty("hibernate.connection.username", user);
properties.setProperty("hibernate.connection.password", pass);
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("hibernate.current_session_context_class","thread");
properties.setProperty("hibernate.hbm2ddl.auto","create");

Configuration config = new Configuration();
config.setProperties(properties);
config.addAnnotatedClass(Player.class);
SessionFactory factory = config.buildSessionFactory();

我的玩家类现在非常简单 - 它有一些参数,例如名称和 ID。它很高兴地将这些转换为正确的表格。

@Entity
@NamedQueries({
@NamedQuery(name="verifyPlayerByUuid", query="SELECT name FROM player WHERE uuid = :uuid"),
@NamedQuery(name="verifyPlayerByName", query="SELECT name FROM player WHERE name = :name"),
@NamedQuery(name="obtainPlayerByUuid", query="FROM player WHERE uuid = :uuid"),
})
public class Player {
// impl here
}

当我运行它时,我得到了这个。请注意,我删除了样板时间戳和 hibernate 包名称,以便为相关消息腾出更多空间。我还随意格式化消息以更好地组织数据 - 这仅涉及添加空格。

[timestamp] [hib].hbm2ddl.SchemaExport   - HHH000227: Running hbm2ddl schema export
[timestamp] [hib].hbm2ddl.SchemaExport - HHH000230: Schema export complete

[timestamp] [hib].SessionFactoryImpl - HHH000177: Error in named query: verifyPlayerByUuid
org.hibernate.hql.internal.ast.QuerySyntaxException:
player is not mapped [SELECT name FROM player WHERE uuid = :uuid]

[timestamp] [hib].SessionFactoryImpl - HHH000177: Error in named query: obtainPlayerByUuid
org.hibernate.hql.internal.ast.QuerySyntaxException:
player is not mapped [FROM player WHERE uuid = :uuid]

[timestamp] [hib].SessionFactoryImpl - HHH000177: Error in named query: verifyPlayerByName
org.hibernate.hql.internal.ast.QuerySyntaxException:
player is not mapped [SELECT name FROM player WHERE name = :name]

最佳答案

您需要使用作为映射实体的类的名称

config.addAnnotatedClass(Player.class);
...
FROM Player WHERE uuid = :uuid

同样,uuid 需要是该类的字段。

关于java - Hibernate 找不到刚刚创建的表的类映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19735902/

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