gpt4 book ai didi

hibernate - 嵌套异常是org.hibernate.exception.SQLGrammarException : could not extract ResultSet , Spring4,Hibernate4

转载 作者:行者123 更新时间:2023-12-01 19:19:57 26 4
gpt4 key购买 nike

它是一个使用 Spring4 和 hibernate4 的项目,我在运行 pronec 时遇到问题,因为它返回无法提取 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

enter image description here

最佳答案

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

关于hibernate - 嵌套异常是org.hibernate.exception.SQLGrammarException : could not extract ResultSet , Spring4,Hibernate4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27720509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com