Hibernate会自动在数据库中创建表格吗? [英] Does Hibernate create tables in the database automatically

查看:99
本文介绍了Hibernate会自动在数据库中创建表格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.vaannila.com/spring/spring-hibernate-integration-1 .html



在阅读本教程时,他们没有提到在数据库中创建表的任何内容。 Hibernate是否会在我指定它们时通过创建表和字段来自动处理它。

这是我的beans配置。

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:p =http://www.springframework.org/schema/p
xsi:schemaLocation =http://www.springframework.org/schema/beans http:// www。 springframework.org/schema/beans/spring-beans.xsd\">

< bean id =viewResolverclass =org.springframework.web.servlet.view.InternalResourceViewResolverp:prefix =/ WEB-INF / jsp /p:suffix =。 jsp/>

< bean id =myDataSourceclass =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =usernamevalue =monwwty/>
< property name =passwordvalue =www/>
< / bean>

< bean id =mySessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =myDataSource/>
< property name =annotatedClasses>
< list>
<值> uk.co.vinoth.spring.domain.User< /值>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto>建立< / prop>
< /道具>
< / property>
< / bean>

< bean id =myUserDAOclass =uk.co.vinoth.spring.dao.UserDAOImpl>
< property name =sessionFactoryref =mySessionFactory/>
< / bean>

< bean name =/ user / *。htmclass =uk.co.vinoth.spring.web.UserController>
< property name =userDAOref =myUserDAO/>
< / bean>

< / beans>


解决方案

您的hibernate.hbm2ddl.auto设置应该定义创建数据库(选项为 validate create update create-drop


http://www.vaannila.com/spring/spring-hibernate-integration-1.html

Upon reading this tutorial, they have not mentioned anything over creating tables in the DB. Does the Hibernate handle it automatically by creating tables and fields once i specify them.

Here is my beans configuration.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"/>
        <property name="username" value="monwwty"/>
        <property name="password" value="www"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>uk.co.vinoth.spring.domain.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="myUserDAO" class="uk.co.vinoth.spring.dao.UserDAOImpl">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    <bean name="/user/*.htm" class="uk.co.vinoth.spring.web.UserController" >
        <property name="userDAO" ref="myUserDAO" />
    </bean>

</beans>

解决方案

your hibernate.hbm2ddl.auto setting should be defining that the database is created (options are validate, create, update or create-drop)

这篇关于Hibernate会自动在数据库中创建表格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆