java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:未映射客户(来自Customer)

时间:2018-09-03 06:06:29

标签: java spring hibernate

  

org.springframework.web.util.NestedServletException:请求   处理失败;嵌套异常为   java.lang.IllegalArgumentException:   org.hibernate.hql.internal.ast.QuerySyntaxException:客户不是   映射(来自客户)

一切正常,为什么会显示此错误

Bean类:

package com.luv2code.springdemo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


//@Table(schema = "web_customer_tracker", name = "customer")
@Table(name="customer")
@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @Column(name="email")
    private String email;

    public Customer() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }

}

DAO Impl类:

    package com.luv2code.springdemo.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.luv2code.springdemo.entity.Customer;

@Repository
public class CustomerDAOImpl implements CustomerDAO {


    @Autowired
    private SessionFactory sessionFactory;
    @Override
    @Transactional
    public List<Customer> getCustomers() {

        //get the current hibernate session
            Session currentSession = sessionFactory.getCurrentSession();


        //List customers = new ArrayList<Customer>();
        //create a query
        Query<Customer> theQuery=currentSession.createQuery("from customer", Customer.class);
                //currentSession.createQuery("from Customer", Customer.class);
        //execute query and get result list
        List<Customer> customers=theQuery.getResultList();
        // return the results


        /*  Customer cus1=new Customer();
        cus1.setEmail("a@gmail.com");
        cus1.setFirstName("Abhishek");
        cus1.setId(10);
        cus1.setLastName("Kumar");
        customers.add(cus1); */

        return customers;
    }

}

DATABASE:

XML:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo" />

    <!-- Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Step 1: Define Database DataSource / connection pool -->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&amp;serverTimezone=UTC" />
        <property name="user" value="springstudent" />
        <property name="password" value="springstudent" /> 

        <!-- these are connection pool properties for C3P0 -->
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxIdleTime" value="30000" />
    </bean>  
    <!-- Step 2: Setup Hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.luv2code.springdemo.entity" />
        <property name="hibernateProperties">
           <props>
              <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
              <prop key="hibernate.show_sql">true</prop>
           </props>
        </property>
   </bean>    

    <!-- Step 3: Setup Hibernate transaction manager -->
    <bean id="myTransactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="myTransactionManager" />


</beans>

5 个答案:

答案 0 :(得分:0)

这是因为您要在客户实体中提供实体名称,因此,在查询中应使用相同的名称(现在在查询中无法引用类的名称)。尝试使用提到的相同实体名称调用查询(也要注意大小写)。

尝试如下:

  Query<Customer> theQuery=
            currentSession.createQuery("from customer", Customer.class);

在这里,我已经匹配了查询中@Entity(name="customer")中提到的实体名称。

答案 1 :(得分:0)

我没有遍历您的所有代码,但是在sessionFactory的{​​{1}} bean中,

尝试将xml更改为<property name="packagesToScan" value="com.luv2code.springdemo.entity" />

您实际的<property name="packagesToScan" value="code.luv2code.springdemo.entity" />实体位于软件包Customer中 ,尽管您使用过code.luv2code.springdemo.entity。因此它正在扫描错误的软件包。这可能是问题所在。

答案 2 :(得分:0)

如果您正在使用检查 C3p0 的包是否适用于休眠,我正在使用以下内容,它对我有用。

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>5.4.26.Final</version>
    </dependency>

与上述答案相反,在 Hibernate 中,即使您添加了 @Table(name="customer") ,它也始终是 HQL 的实体名称

 Query<Customer> theQuery=
        currentSession.createQuery("from Customer", Customer.class);

如果这仍然不适合您,请告诉我

答案 3 :(得分:0)

Customer

@Entity --> hibernate 默认为 @Entity(name = "Customer") 尽管您的数据库表是 customer 它读作 Customer 因为带注释的类 hibernate 配置为 Customer

所以更新,

List<Customer> li = session.createQuery("from Customer").list();

答案 4 :(得分:0)

试试这个:

Query theQuery=currentSession.createQuery("from Customer");