本文介绍了为什么Hibernate会在我设置hbm2ddl.auto创建时抛出一个SQLGrammarException说表/视图不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直在试验hibernate和spring以及servlet。现在,我卡住了。为什么我得到这个异常?当hbm2ddl.auto设置为创建时,我认为表格会自动创建。I've been experimenting with hibernate and spring and servlets. Now, I'm stuck. Why am I getting this exception? I thought tables would be created automatically when hbm2ddl.auto is set to create. appicationContext.xmlappicationContext.xml<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" /> <property name="url" value="jdbc:derby://localhost:1527/db;create=true" /></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <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.javaUserDetails.javapackage org.springpractice.domain;@Entitypublic class UserDetails { @Id @GeneratedValue protected int id; protected String name; protected String email; // setters/getters ...} Main.javaMain.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("[email protected]"); user.setName("Bob Smith"); ApplicationContext beanFactory = WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext()); SessionFactory sessionFactory = (SessionFactory) beanFactory.getBean("sessionFactory"); Session session = sessionFactory.openSession(); session.save(user); session.close(); }} 异常Exceptionorg.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)...推荐答案<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <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>顺便提一下,配置属性的方法如下:BTW there is a simpler way to configure properties as follows <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> 这篇关于为什么Hibernate会在我设置hbm2ddl.auto创建时抛出一个SQLGrammarException说表/视图不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-06 02:33