嵌套异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet,Spring4,Hibernate4

时间:2014-12-31 11:31:04

标签: hibernate spring-mvc resultset hibernate-4.x spring-4

它是一个使用Spring4和hibernate4的项目,我在运行pronect时遇到问题,因为它返回无法提取ResultSet 我已经创建了数据库,并像往常一样连接。但它返回错误。我怎么能解决它。

HomeController代码

@Controller
public class HomeController {

@Autowired
private UserDao userDAO;

@RequestMapping(value="/")
public ModelAndView home(){
List<User> listUser=userDAO.list();
ModelAndView model=new ModelAndView("home");
model.addObject("userList",listUser);
return model;   
}

UserDAOImplementation Code

public class UserDAOImpl implements UserDAO {

private SessionFactory sessionFactory;
public UserDAOImpl (SessionFactory sessionFactory){

    this.sessionFactory=sessionFactory;
}

@Override
@Transactional
public List<User> list() {

    @SuppressWarnings("unchecked")
    List<User>listUser =(List<User>)sessionFactory.getCurrentSession().createCriteria(User.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

    return listUser;
}   
}

User.hbm.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 <hibernate-mapping package="com.spring4hibernate4test.org.model">
 <class name="User" table="USERS" >
 <id name="id" column="USER_ID">
 <generator class="native"/>
 </id>
<property name="username" column="USERNAME" />
<property name="password" column="PASSWORD" />
<property name="email" column="EMAIL" />
 </class>

 </hibernate-mapping>

servlet-context.xml代码

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


<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<context:component-scan base-package="com.spring4hibernate4test.org" />



<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/usersdb"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean> 

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

<tx:annotation-driven />
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="userDao" class="com.spring4hibernate4test.org.dao.UserDAOImpl">
    <constructor-arg>
        <ref bean="sessionFactory" />
    </constructor-arg>
</bean> 

根本原因

 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'usersdb.USERS' doesn't exist

enter image description here

1 个答案:

答案 0 :(得分:5)

我记得,默认情况下,MySQL表/列名在Linux环境中是区分大小写的,因此请尝试相应地更新User.hbm.xml