问题描述
我正在尝试使用休眠模式连接到Postgres,但是我遇到了未映射表的异常.这是代码:
I'm trying to connect to Postgres with hibernate however I'm getting exception that table is not mapped. Here is code:
hibernate.cfg.xml
hibernate.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:
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>
错误:
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
主要:
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);
}
}
在我的数据库中,有一个名为"Users"的表,其表中的列类似于UsersEntity批注中的列.我认为,映射已正确完成,但是我仍然遇到相同的错误.感谢您的帮助
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
推荐答案
错误很明显" UsersModel未映射".这意味着您试图在查询中使用尚未进行任何映射的类.
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 :
List<UsersModel> list = session.createQuery("select login from UsersModel").list();
但是映射到Users表的实体类是UsersEntity
:
But the entity class mapped to Users table is UsersEntity
:
@Entity
@Table(name = "Users")
public class UsersEntity
您应该执行以下查询:
List<UsersEntity> list = session.createQuery("select login from UsersEntity").list();
// Map from UsersEntity to UsersModel if needed
这篇关于休眠异常:未映射表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!