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

时间:2011-08-03 12:46:33

标签: hibernate spring integration

我正在尝试使用 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>

有人请帮助我解决错误

1 个答案:

答案 0 :(得分:0)

尝试以下解决方案:

  • 溶液#1

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

  • 溶液#2

    在员工表上授予插入/更新/删除/选择。

授予选择,插入,更新,删除员工“USER_NAME”