不使用Hibernate创建表

时间:2015-09-24 21:50:01

标签: spring hibernate

我使用Spring MVC和Hibernate将数据插入数据库,但是当我第一次运行我的项目时,它不使用hibernate生成表。

我的实体类:

package com.cts.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="RegistrationDetails")
public class RegistrationDetails 
{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private String RegistrationId;

    @Column
    private String firstname;
    @Column
    private String lastname;
    @Column
    private String address;
    @Column
    private String emailid;
    @Column
    private String password;
    @Column
    private Long contactno;

    public String getRegistrationId() {
        return RegistrationId;
    }
    public void setRegistrationId(String registrationId) {
        RegistrationId = registrationId;
    }


    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getEmailid() {
        return emailid;
    }
    public void setEmailid(String emailid) {
        this.emailid = emailid;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Long getContactno() {
        return contactno;
    }
    public void setContactno(Long contactno) {
        this.contactno = contactno;
    }


}

My Dispatcher Servlet:

<?xml version="1.0" encoding="UTF-8"?><beans
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd  
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd"

    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans">
        <mvc:annotation-driven />
    <context:annotation-config />

    <context:component-scan base-package="com.cts.*" />

    <aop:aspectj-autoproxy />

    <mvc:resources location="/" mapping="/**" />
    <tx:annotation-driven proxy-target-class="true" />
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        id="DataSource">

        <property value="com.mysql.jdbc.Driver" name="driverClassName" />

        <property value="jdbc:mysql://localhost:3306/opop"
            name="url" />

        <property value="root" name="username" />

        <property value="root" name="password" />

    </bean>

    <!-- <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        setting maximum upload size
        <property name="maxUploadSize" value="1000000000" />
    </bean> -->

    <bean
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        id="newSessionFactory">
        <property name="dataSource" ref="DataSource" />
        <property name="annotatedClasses">

            <list>
                <value>com.cts.entity.RegistrationDetails</value>
                <value>com.cts.entity.LoginDetails</value>
                <value>com.cts.entity.RegisterationDetails</value>

            </list>

        </property>
        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

                <prop key="hibernate.hbm2ddl.auto">update</prop>

                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        id="transactionManager">

        <property name="sessionFactory" ref="newSessionFactory" />

    </bean>

    <bean id="h" class="org.springframework.orm.hibernate3.HibernateTemplate"
        autowire="constructor" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="message" />
    </bean> -->


</beans>


Hibernate not generating tables.I am inserting data into database using Spring MVC and Hibernate but when i am first time running my project its not generating tables using hibernate.

2 个答案:

答案 0 :(得分:0)

在hibernateProperties中,将属性hibernate.hbm2ddl.auto设置为create-drop

<property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>

                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>

这将在您第一次运行应用程序时创建表。在后续运行中,如果要更新任何现有表,则可以将属性设置为更新。但万一,如果你添加任何新表,那么你需要再次给出create-drop。

答案 1 :(得分:0)

尝试更改

<context:component-scan base-package="com.cts.*" /><context:component-scan base-package="com.cts" />.

您可能还需要

<tx:annotation-driven transaction-manager="transactionManager" />.

只需添加Gaurov给出的答案,hibernate.hbm2ddl.auto设置为&#34;更新&#34;将为您的新实体(不在db上)创建新表,create-drop将在您停止应用后删除它们。