Java 休眠异常:表未映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33531210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate exception : table is not mapped
提问by DisQ
I'm trying to connect to Postgres with hibernate however I'm getting exception that table is not mapped. Here is code:
我正在尝试使用 hibernate 连接到 Postgres,但是我遇到了表未映射的异常。这是代码:
hibernate.cfg.xml
休眠文件.cfg.xml
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">passwrd</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/dbname</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<mapping class="backend.hibernate.models.UsersEntity"/>
</session-factory>
UsersEntity:
用户实体:
@Entity
@Table(name = "Users")
@Getter
@Setter
public class UsersEntity {
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@Column(name = "login")
private String login;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
}
pom:
绒球:
<properties>
<hibernate.version>5.0.3.Final</hibernate.version>
<postgresql.version>9.4-1200-jdbc4</postgresql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- The tutorials use the PostgreSQL 9.3.5 database -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
error:
错误:
Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: UsersModel is not mapped [select login from UsersModel]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
at Fronted.Main.main(Main.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: UsersModel is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:76)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
... 13 more
main:
主要的:
public class Main {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration
.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
Session session = sessionFactory.openSession();
session.beginTransaction();
List<UsersModel> list = session.createQuery("from UsersEntity").list();
session.close();
System.out.println(list);
}
}
In my database I have table called "Users" with columns like in the UsersEntity annotations. I think, that mapping is done properly however I still get the same error. Thanks for the help
在我的数据库中,我有一个名为“Users”的表,其中的列类似于 UsersEntity 注释。我认为,该映射已正确完成,但是我仍然遇到相同的错误。谢谢您的帮助
回答by Ga?l J
The error is pretty clear "UsersModel is not mapped". This means you are trying to use a class in a query for which you havent done any mapping.
错误很明显“ UsersModel 未映射”。这意味着您正在尝试在未进行任何映射的查询中使用类。
You are querying on UsersModel
with following :
您正在查询UsersModel
以下内容:
List<UsersModel> list = session.createQuery("select login from UsersModel").list();
But the entity class mapped to Users table is UsersEntity
:
但是映射到用户表的实体类是UsersEntity
:
@Entity
@Table(name = "Users")
public class UsersEntity
You should do the following query :
您应该执行以下查询:
List<UsersEntity> list = session.createQuery("select login from UsersEntity").list();
// Map from UsersEntity to UsersModel if needed
回答by Yago Lasse
Maybe into the hibernate.cfg.xml you are mapping a wrong class name.
也许在 hibernate.cfg.xml 中您映射了错误的类名。
In your xml:
在您的 xml 中:
<mapping class="backend.hibernate.models.UsersModel"/>
In your Java class:
在您的 Java 类中:
public class UsersEntity
Try to change your xml like this:
尝试像这样更改您的 xml:
<mapping class="backend.hibernate.models.UsersEntity"/>
回答by Ahmed Nawaz
As you are using entity name as
当您使用实体名称作为
UsersEntity
so please change in hibernate.cfg.xml from
所以请更改 hibernate.cfg.xml 从
<mapping class="backend.hibernate.models.UsersModel"/>
to
到
<mapping class="backend.hibernate.models.UsersEntity"/>
and change in main: from
和主要变化:从
List<UsersModel> list = session.createQuery("select login from UsersModel").list();
to
到
List<UsersEntity> list = session.createQuery("select login from UsersEntity").list();
回答by Oleksii Kyslytsyn
It can be added programmatically as well via:
它也可以通过以下方式以编程方式添加:
Configuration configuration = new Configuration();
configuration.configure("hibernate-local-query-test.cfg.xml");
configuration.addAnnotatedClass(ClassName.class);