简体   繁体   中英

Hibernate exception : table is not mapped

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

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

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.

You are querying on UsersModel with following :

List<UsersModel> list = session.createQuery("select login from UsersModel").list();

But the entity class mapped to Users table is 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

As you are using entity name as

UsersEntity

so please change in hibernate.cfg.xml from

<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();

Maybe into the hibernate.cfg.xml you are mapping a wrong class name.

In your xml:

<mapping class="backend.hibernate.models.UsersModel"/>

In your Java class:

public class UsersEntity

Try to change your xml like this:

<mapping class="backend.hibernate.models.UsersEntity"/>

It can be added programmatically as well via:

Configuration configuration = new Configuration();
configuration.configure("hibernate-local-query-test.cfg.xml");
configuration.addAnnotatedClass(ClassName.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM