使用 hibernate.hbm2ddl.auto 将表保存到数据库时出现问题,出现运行时异常

发布于 2024-11-27 20:00:38 字数 4885 浏览 0 评论 0原文

我正在尝试使用 hibernate 将我的 java 对象映射到 MySQL 数据库,但出现以下异常

Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not retrieve snapshot: [hibernate.Employee#125]; SQL [select employee_.Id, employee_.version as version0_, employee_.Name as Name0_, employee_.Age as Age0_, employee_.Salary as Salary0_ from Employee employee_ where employee_.Id=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not retrieve snapshot: [hibernate.Employee#125]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
    at hibernate.EmployeeDao.saveOrUpdate(EmployeeDao.java:41)
    at hibernate.SpringHibernateTest.main(SpringHibernateTest.java:24)
Caused by: org.hibernate.exception.SQLGrammarException: could not retrieve snapshot: [hibernate.Employee#125]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.persister.entity.AbstractEntityPersister.getDatabaseSnapshot(AbstractEntityPersister.java:1137)
    at org.hibernate.engine.StatefulPersistenceContext.getDatabaseSnapshot(StatefulPersistenceContext.java:292)
    at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:211)
    at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:531)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:103)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1282)
    at $Proxy0.saveOrUpdate(Unknown Source)
    at hibernate.EmployeeDao$2.doInHibernate(EmployeeDao.java:37)

我使用了 hibernate.hbm2ddl.auto = update 这是我的 spring-hibernate。 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/subhash"/>
    <property name="username" value="root"/>
    <property name="password" value="pass"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
        <list>
            <value>employee.hbm.xml</value>
        </list>
    </property>
    <!-- <property name="hibernateProperties">
        <value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
    </property> -->
    <property name="hibernateProperties">
        <props>
        <prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
        <ref bean="mySessionFactory"/>
    </property>
</bean>
<bean id="employeeDao" class="hibernate.EmployeeDao">
    <property name="hibernateTemplate">
        <ref bean="hibernateTemplate"/>    
    </property>
</bean>
</beans>

请有人帮我解决该错误

I am trying to map my java object to MySQL database using hibernate but i am getting following exception

Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not retrieve snapshot: [hibernate.Employee#125]; SQL [select employee_.Id, employee_.version as version0_, employee_.Name as Name0_, employee_.Age as Age0_, employee_.Salary as Salary0_ from Employee employee_ where employee_.Id=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not retrieve snapshot: [hibernate.Employee#125]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
    at hibernate.EmployeeDao.saveOrUpdate(EmployeeDao.java:41)
    at hibernate.SpringHibernateTest.main(SpringHibernateTest.java:24)
Caused by: org.hibernate.exception.SQLGrammarException: could not retrieve snapshot: [hibernate.Employee#125]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.persister.entity.AbstractEntityPersister.getDatabaseSnapshot(AbstractEntityPersister.java:1137)
    at org.hibernate.engine.StatefulPersistenceContext.getDatabaseSnapshot(StatefulPersistenceContext.java:292)
    at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:211)
    at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:531)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:103)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1282)
    at $Proxy0.saveOrUpdate(Unknown Source)
    at hibernate.EmployeeDao$2.doInHibernate(EmployeeDao.java:37)

I have used hibernate.hbm2ddl.auto = update here is my spring-hibernate.xml file

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/subhash"/>
    <property name="username" value="root"/>
    <property name="password" value="pass"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
        <list>
            <value>employee.hbm.xml</value>
        </list>
    </property>
    <!-- <property name="hibernateProperties">
        <value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
    </property> -->
    <property name="hibernateProperties">
        <props>
        <prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
        <ref bean="mySessionFactory"/>
    </property>
</bean>
<bean id="employeeDao" class="hibernate.EmployeeDao">
    <property name="hibernateTemplate">
        <ref bean="hibernateTemplate"/>    
    </property>
</bean>
</beans>

CAn some one please help me in resolving the error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2024-12-04 20:00:38

尝试以下解决方案:

  • 解决方案#1

    尝试将 hibernate 配置属性“hibernate.hbm2ddl.auto”更改为“create”。

  • 解决方案#2

    在 Employee 表上授予插入/更新/删除/选择权限。

将员工的选择、插入、更新、删除权限授予“USER_NAME”

Try following solutions :

  • Solution#1

    Try changing hibernate configuration property "hibernate.hbm2ddl.auto" to "create".

  • Solution#2

    Grant insert/update/delete/select on Employee table.

GRANT SELECT,INSERT,UPDATE,DELETE ON EMPLOYEE TO "USER_NAME"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文