Hibernate error - QuerySyntaxException: users is not mapped [from users]

前端 未结 19 2205
感动是毒
感动是毒 2020-11-30 20:14

I\'m trying to get a list of all the users from \"users\" table and I get the following error:

org.hibernate.hql.internal.ast.QuerySyntaxException: users is          


        
相关标签:
19条回答
  • 2020-11-30 21:00

    org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [from users]

    This indicates that hibernate does not know the User entity as "users".

    @javax.persistence.Entity
    @javax.persistence.Table(name = "Users")
    public class User {
    

    The @Table annotation sets the table name to be "Users" but the entity name is still referred to in HQL as "User".

    To change both, you should set the name of the entity:

    // this sets the name of the table and the name of the entity
    @javax.persistence.Entity(name = "Users")
    public class User implements Serializable{
    

    See my answer here for more info: Hibernate table not mapped error

    0 讨论(0)
  • 2020-11-30 21:01

    Also make sure that the following property is set in your hibernate bean configuration:

    <property name="packagesToScan" value="yourpackage" />
    

    This tells spring and hibernate where to find your domain classes annotated as entities.

    0 讨论(0)
  • 2020-11-30 21:03

    In a Spring project: I typed wrong hibernate.packagesToScan=com.okan.springdemo.entity and got this error. Now it's working well.

    0 讨论(0)
  • 2020-11-30 21:06

    If you are using xml configuration, you'll need something like the following in an applicationContext.xml file:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" lazy-init="default" autowire="default" dependency-check="default">
    <property name="dataSource">
      <ref bean="dataSource" />
    </property>
    <property name="annotatedClasses">
      <list>
        <value>org.browsexml.timesheetjob.model.PositionCode</value>
      </list>
    
    0 讨论(0)
  • 2020-11-30 21:07

    Just to share my finding. I still got the same error even if the query was targeting the correct class name. Later on I realised that I was importing the Entity class from the wrong package.

    The problem was solved after I change the import line from:

    import org.hibernate.annotations.Entity;
    

    to

    import javax.persistence.Entity;
    
    0 讨论(0)
  • 2020-11-30 21:07

    Added @TABLE(name = "TABLE_NAME") annotation and fixed. Check your annotations and hibernate.cfg.xml file. This is the sample entity file that works:

    import javax.persistence.*;
    
    @Entity
    @Table(name = "VENDOR")
    public class Vendor {
    
        //~ --- [INSTANCE FIELDS] ------------------------------------------------------------------------------------------
        private int    id;
        private String name;
    
        //~ --- [METHODS] --------------------------------------------------------------------------------------------------
        @Override
        public boolean equals(final Object o) {    
            if (this == o) {
                return true;
            }
    
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
    
            final Vendor vendor = (Vendor) o;
    
            if (id != vendor.id) {
                return false;
            }
    
            if (name != null ? !name.equals(vendor.name) : vendor.name != null) {
                return false;
            }
    
            return true;
        }
    
        //~ ----------------------------------------------------------------------------------------------------------------
        @Column(name = "ID")
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Id
        public int getId() {
            return id;
        }
    
        @Basic
        @Column(name = "NAME")
        public String getName() {
    
            return name;
        }
    
        public void setId(final int id) {
            this.id = id;
        }
    
        public void setName(final String name) {    
            this.name = name;
        }
    
        @Override
        public int hashCode() {
            int result = id;
            result = 31 * result + (name != null ? name.hashCode() : 0);
            return result;
        }
    }
    
    0 讨论(0)
提交回复
热议问题