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"&开发者_如何学Gogt;
<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
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"
精彩评论