为啥当我将 hbm2ddl.auto 设置为创建时,Hibernate 会抛出 SQLGrammarException 说表/视图不存在?

Posted

技术标签:

【中文标题】为啥当我将 hbm2ddl.auto 设置为创建时,Hibernate 会抛出 SQLGrammarException 说表/视图不存在?【英文标题】:Why is Hibernate throwing a SQLGrammarException saying table/view does not exist when I've set hbm2ddl.auto to create?为什么当我将 hbm2ddl.auto 设置为创建时,Hibernate 会抛出 SQLGrammarException 说表/视图不存在? 【发布时间】:2012-05-26 13:46:26 【问题描述】:

我一直在尝试使用 hibernate、spring 和 servlet。现在,我被困住了。为什么我会收到此异常?我认为当 hbm2ddl.auto 设置为创建时会自动创建表。

appicationContext.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.java

package org.springpractice.domain;

@Entity
public 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】:

尝试使用@Table("USERDETAILS") 注释您的实体,并使用@Column 注释您的所有字段。

参考http://www.vaannila.com/spring/spring-hibernate-integration-1.html Does Hibernate create tables in the database automatically Hibernate hbm2ddl.auto possible values and what they do?

【讨论】:

【参考方案2】:

属性名称应以hibernate为前缀。

<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>

顺便说一句,有一种更简单的方法来配置属性,如下所示

    <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> 

【讨论】:

以上是关于为啥当我将 hbm2ddl.auto 设置为创建时,Hibernate 会抛出 SQLGrammarException 说表/视图不存在?的主要内容,如果未能解决你的问题,请参考以下文章

为啥即使使用 hibernate.hbm2ddl.auto" value="create",hibernate 也不会自动创建表?

如何在 Wildfly 部署时设置 hibernate.hbm2ddl.auto 不使用 persistence.xml

Hibernate、MySQL 视图和 hibernate.hbm2ddl.auto = 验证

在 Hibernate 5 中未使用 hbm2ddl.auto=update 创建表

序列问题为啥两个实体在使用 hbm2ddl 生成模式时共享相同的序列?

hibernate.hbm2ddl.auto Hibernate 如何决定何时创建或更新 ddl?