【问题标题】:nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet ,Spring4,Hibernate4嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet ,Spring4,Hibernate4
【发布时间】:2015-02-27 12:10:04
【问题描述】:

它是一个使用 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;   
}

UserDAO实现代码

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

【问题讨论】:

  • 通过在数据库中创建缺失的表? “表'usersdb.USERS'不存在”有什么不清楚的地方?
  • 那张桌子在那里。这让我感到困惑。这可能是连接问题@JBNizet

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


【解决方案1】:

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

【讨论】:

  • 好的,就是这样。我想知道为什么会这样。非常感谢您的回复。
猜你喜欢
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 2015-06-25
相关资源
最近更新 更多