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

时间:2013-11-01 21:45:52

标签: java hibernate hibernate-annotations

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

1 个答案:

答案 0 :(得分:2)

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

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

同样,uuid必须是该类的一个字段。