我已经为我创建了我的数据库模式。它能够很好地创建表,但命名查询无法编译,因为它声称对象未映射。
整个应用程序是注释和配置驱动的:没有.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]