本文将带您了解关于当我将hbm2ddl.auto设置为create时,为什么Hibernate抛出SQLGrammarException表示表/视图不存在?的新内容,另外,我们还将为您提供关于Caus
本文将带您了解关于当我将hbm2ddl.auto设置为create时,为什么Hibernate抛出SQLGrammarException表示表/视图不存在?的新内容,另外,我们还将为您提供关于Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet、could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query、hibernate hbm2ddl.auto 问题、Hibernate hbm2ddl.auto默认值的实用信息。
本文目录一览:- 当我将hbm2ddl.auto设置为create时,为什么Hibernate抛出SQLGrammarException表示表/视图不存在?
- Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
- could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
- hibernate hbm2ddl.auto 问题
- Hibernate hbm2ddl.auto默认值
当我将hbm2ddl.auto设置为create时,为什么Hibernate抛出SQLGrammarException表示表/视图不存在?
我一直在尝试hibernate,Spring和Servlet。现在,我被卡住了。为什么会出现此异常?我认为将hbm2ddl.auto设置为create时会自动创建表。
appicationContext.xml
<bean id="dataSource"> <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" /> <property name="url" value="jdbc:derby://localhost:1527/db;create=true" /></bean><bean id="sessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="org.springpractice.domain" /> <property name="hibernateProperties"> <props> <prop key="dialect"> org.hibernate.dialect.DerbyTenSevenDialect</prop> <prop key="show_sql">true</prop> <prop key="cache.provider_class"> org.hibernate.cache.NoCacheProvider</prop> <prop key="hbm2ddl.auto">create</prop> </props> </property> </bean>
UserDetails.java
package org.springpractice.domain;@Entitypublic class UserDetails { @Id @GeneratedValue protected int id; protected String name; protected String email; // setters/getters ...}
Main.java
@WebServlet("/")public class Main extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); pw.println("hello from servlet"); UserDetails user = new UserDetails(); user.setEmail("bob.smith@gmail.com"); user.setName("Bob Smith"); ApplicationContext beanFactory = WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext()); SessionFactory sessionFactory = (SessionFactory) beanFactory.getBean("sessionFactory"); Session session = sessionFactory.openSession(); session.save(user); session.close(); }}
例外
org.hibernate.exception.SQLGrammarException: Table/View ''USERDETAILS'' does not exist.org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:146)org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)$Proxy10.prepareStatement(Unknown Source)...
答案1
小编典典属性名称应以hibernate作为前缀。
<bean id="sessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="org.springpractice.domain" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.DerbyTenSevenDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cache.provider_class"> org.hibernate.cache.NoCacheProvider</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean>
顺便说一句,有一种更简单的方法来配置属性,如下所示
<property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect hibernate.show_sql=true hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider hibernate.hbm2ddl.auto"=create </value> </property>
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:281)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy91.save(Unknown Source)
at com.qingmu.seller.reposity.ProductCategoryReposityTest.saveTest(ProductCategoryReposityTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:67)
at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:95)
at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:520)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:192)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:108)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:702)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:688)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:314)
at com.sun.proxy.$Proxy86.persist(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:554)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:371)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:204)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:657)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:621)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 38 more
Caused by: java.sql.SQLSyntaxErrorException: FUNCTION seller.nextval does not exist
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57)
... 72 more
使用 springDataJPA 时候,因为数据库方言设置不正确,报错为上面的信息.
修改一下数据库中配置的方言
jpa:
hibernate:
ddl-auto: create
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
show-sql: true
解决这个问题,
还有一种就是数据库中的字段和实体类中的名称对应不上,也会报这个错误
could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
Cloud Foundry连上了数据库,但是报了一个异常: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
源码是:
String hql = " FROM TUser as u WHERE u.username=? and u.password=?";
List<TUser> all = super.getHibernateTemplate().find(hql, new Object[] { user.getUsername(), user.getPassword() });
项目数据库层采用Hibernate,同样的代码在本地完全正常,但是部署到Cloud Foundry就报出了这个错误 ,很郁闷 ,想请问Cloud Foundry不支持Hibernate么?还是怎么回事,有没有人和我遇到同样的问题啊。。。求救啊。。。。
hibernate hbm2ddl.auto 问题
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
validate 加载 hibernate 时,验证创建数据库表结构
create 每次加载 hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载 hibernate 时创建,退出是删除表结构
update 加载 hibernate 自动更新数据库结构
当使用注释时候:
mysql
@Column(name="`order`", columnDefinition="int default ''0''")
columnDefinition 必须写完整,否者不报错也不生成
order 是关键字,必须用 ` 包起来
Hibernate hbm2ddl.auto默认值
的默认值是多少
hibernate.hbm2ddl.auto
在hibernatecfg文件映射中
是否可以删除
<property name="hibernate.hbm2ddl.auto">update</property>
从配置文件映射
如果我删除此属性是否会影响我的数据库
???
答案1
小编典典创建SessionFactory时,自动将架构DDL验证或导出到数据库。使用create-drop时,显式关闭SessionFactory时将删除数据库架构。
validate | update | create | create-drop
- 验证现有模式
- 更新-创建后仅更新您的架构
- 每次创建-创建架构
今天关于当我将hbm2ddl.auto设置为create时,为什么Hibernate抛出SQLGrammarException表示表/视图不存在?的介绍到此结束,谢谢您的阅读,有关Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet、could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query、hibernate hbm2ddl.auto 问题、Hibernate hbm2ddl.auto默认值等更多相关知识的信息可以在本站进行查询。
本文标签: