简体   繁体   English

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

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

I have hibernate creating my database schema for me. 我有hibernate为我创建我的数据库架构。 It's able to create the tables just fine, but the named queries do not compile because it claims the objects aren't mapped. 它能够很好地创建表,但命名查询不能编译,因为它声称对象没有映射。

The entire application is annotation and config driven: there are no .xml files. 整个应用程序是注释和配置驱动:没有.xml文件。

Here is the relevant config portions: 以下是相关的配置部分:

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

My player class is pretty barebones right now - it has some parameters like name and id. 我的播放器类现在非常准确 - 它有一些参数,如名称和ID。 It happily translates these into a proper table. 它愉快地将这些翻译成一张合适的桌子。

@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
}

When I run it, I get this. 当我运行它时,我明白了。 Note that I trimmed out the boilerplate timestamp and hibernate package names to make more room for the relevant messages. 请注意,我修剪了样板时间戳和休眠包名称,以便为相关消息腾出更多空间。 I also took liberties in formatting the message to better organize the data - this involved only the addition of whitespace. 我还对格式化消息以更好地组织数据采取了自由 - 这只涉及添加空格。

[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]

You need to use the name of the class which is a mapped entity 您需要使用作为映射实体的类的名称

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

Similarly, uuid needs to be a field of that class. 同样, uuid需要成为该类的一个领域。

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

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