我遇到以下错误:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: AccountSettings is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3695)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3584)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:272)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192)
... 10 more
Process finished with exit code 1
“我的实体”如下所示:/ 省略了吸气剂和吸气剂,但它们在那里 /
@Entity
@Table(name="accounts_settings")
public class AccountSettings {
@Id
@Column(name="id")
private Integer id;
@Column(name="hash")
private String hash;
@Column(name="location_visibility_level")
private String location_visibility_level;
@Column(name="ip_masking_level")
private String ip_masking_level;
}
DAO:
公共类AccountSettingsDAO {
public List<AccountSettings> getAccountSettings(){
try (Session session = HibernateIUtils.getSessionFactory().openSession()) {
return session.createQuery("SELECT location_visibility_level, ip_masking_level FROM AccountSettings", AccountSettings.class).list();
}
}
}
`AccountSettings` is the entity name and `accounts_settings` is the table name.
我还尝试将表映射添加到hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/DBNAME</property>
<property name="hibernate.connection.username">USERNAME</property>
<property name="connection.password">PASSWORD</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="packagesToScan">com.package.name.entity</property>
<mapping class="com.package.name.entity.AccountSettings"/>
</session-factory>
</hibernate-configuration>
我已经检查表名称和列名称在实体中是否正确。这里可能出什么问题了?
我正在使用最新的Hibernate依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>
这是引发错误的地方:
public static AccountSettings getAccountSettings(String hash) {
try {
AccountSettingsDAO accountSettingsDAO = new AccountSettingsDAO();
List<com.packagname.AccountSettings> accountSettingsList= accountSettingsDAO.getAccountSettings(); //This is where the exception gets thrown
}catch(HibernateException he){
//we catch the same exception here
}
}
也尝试降级到其他版本。怎么了?我该如何解决?