我正在尝试使用hibernate连接到Postgres但是我得到的例外是表没有映射。这是代码:
的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:
@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注释中的列。我认为,映射已正确完成,但我仍然得到相同的错误。谢谢你的帮助
答案 0 :(得分:2)
错误很明显“ UsersModel未映射”。这意味着您正在尝试在查询中使用您尚未进行任何映射的类。
您正在UsersModel
查询以下内容:
List<UsersModel> list = session.createQuery("select login from UsersModel").list();
但是映射到Users表的实体类是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
答案 1 :(得分:1)
当您使用实体名称
时UsersEntity
所以请从
更改hibernate.cfg.xml<mapping class="backend.hibernate.models.UsersModel"/>
到
<mapping class="backend.hibernate.models.UsersEntity"/>
并更改为main:from
List<UsersModel> list = session.createQuery("select login from UsersModel").list();
到
List<UsersEntity> list = session.createQuery("select login from UsersEntity").list();
答案 2 :(得分:0)
也许你在hibernate.cfg.xml中映射了一个错误的类名。
在你的xml中:
<mapping class="backend.hibernate.models.UsersModel"/>
在您的Java类中:
public class UsersEntity
尝试像这样更改你的xml:
<mapping class="backend.hibernate.models.UsersEntity"/>
答案 3 :(得分:0)
也可以通过以下方式以编程方式添加:
Configuration configuration = new Configuration();
configuration.configure("hibernate-local-query-test.cfg.xml");
configuration.addAnnotatedClass(ClassName.class);