I have hibernate creating my database schema for me. 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.

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. 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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top