繁体   English   中英

Hibernate无法为刚刚创建的表的类找到映射

[英]Hibernate cannot find mappings for classes for tables it just created

我有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
}

当我运行它时,我明白了。 请注意,我修剪了样板时间戳和休眠包名称,以便为相关消息腾出更多空间。 我还对格式化消息以更好地组织数据采取了自由 - 这只涉及添加空格。

[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需要成为该类的一个领域。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM