不要休眠自动生成表吗?

时间:2017-12-14 11:41:28

标签: java hibernate spring-mvc jpa

为什么不自动创建表

数据源XML                       

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close" >

        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/otostore" />
        <property name="user" value="root" />
        <property name="password" value="12345" />

        <!-- C3P0 properties -->
        <property name="minPoolSize" value="5"/>
        <property name="maxPoolSize" value="300" />
        <property name="maxIdleTime" value="3600" />
        <property name="maxStatements" value="500" />
        <property name="acquireIncrement" value="5" />
        <property name="idleConnectionTestPeriod" value="600"/>
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <!--<property name="persistenceUnitName" value="persistenceUnit" />  -->  
         <property name="dataSource" ref="dataSource" />
         <property name="packagesToScan" value="com.otostore.model" />

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQ5LDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.hbm2ddl.show">true</prop> 

                <!--<prop key="hibernate.hbm2ddl.auto">update</prop> -->
            </props>
        </property> 
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"  />
    </bean>
</beans>

Pojo Class

public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id;

    @NotNull
    @Size(max = 50,min = 3)
    private String firstName;

    @NotNull
    @Size(max = 50,min = 3)
    private String lastName;

    @Column(unique = true)
    @NotNull
    @Size(max = 50, min = 6)
    private String profileName;

    @NotNull
    @Size(max = 20,min = 10)
    private String  phone;

    @NotNull
    @Size(max = 50,min = 6)
    private String email;

    @NotNull
    @Size(max = 12,min=8)
    private String password;

    public Customer() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    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 getProfileName() {
        return profileName;
    }

    public void setProfileName(String profileName) {
        this.profileName = profileName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return new 
     StringBuilder("Customer[").append("id").append(id)
                .append(",firstName=").append(firstName)
                .append(",lastName=").append(profileName)
                .append(",profileName=").append(profileName)
                .append(",phone=").append(phone)
                .append(",email=").append(email)
                .append(",password=").append(password)
                .append("]").toString();`enter code here`
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((profileName == null) ? 0 : 
              profileName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Customer other = (Customer) obj;
        if (profileName == null) {
            if (other.profileName != null)
                return false;
        } else if (!profileName.equals(other.profileName))
            return false;
        return true;
    }
}

Pojo Class

public class Vehicle {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Size(max = 30)
    private String make;

    @NotNull
    @Size(max = 30)
    private String model;

    @NotNull
    private int year;
    private double price;

    @Size(max =500)
    private String description;

    @Size(max = 500)
    private String imagePath;

    @Column(name = "addDate" , updatable = false)
    private Date addDate;
    private Date updateDate;

    private String isNew;
    private String fuel;
    private String vendorName;

    @ManyToOne(fetch = FetchType.EAGER)
    private Vendor vendor;

    public Vehicle() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImagePath() {
        return imagePath;
    }`enter code here`

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public Date getAddDate() {
        return addDate;
    }

    public void setAddDate(Date addDate) {
        this.addDate = addDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getIsNew() {
        return isNew;
    }

    public void setIsNew(String isNew) {
        this.isNew = isNew;
    }

    public String getFuel() {
        return fuel;
    }

    public void setFuel(String fuel) {
        this.fuel = fuel;
    }

    public String getVendorName() {
        return vendorName;
    }

    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }

    public Vendor getVendor() {
        return vendor;
    }

    public void setVendor(Vendor vendor) {
        this.vendor = vendor;
    }

    @Override
    public String toString() {
        return new StringBuilder("Vehicle [")
                .append("id=").append(id)
                .append(",make=").append(make)
                .append(",model=").append(model)
                .append(",year=").append(year)
                .append(",price=").append(price)
                .append(",description=").append(description)
                .append(",imagePath=").append(imagePath)
                .append(",addDate=").append(addDate)
                .append(",updateDate=").append(updateDate)
                .append(",isNew=").append(isNew)
                .append(",fuel=").append(fuel)
                .append(",vendorName").append(vendorName)
                .append("]").toString();                
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : 
                           id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Vehicle other = (Vehicle) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

在您的POJO课程之上,您还没有提供@Entity注释,这样可以使用表格映射您的课程。

同样使用@Entity,您可以提供另一个注释,即@Table(name =&#34; table_name&#34;),您可以在其中定义表名。

&#34;创建&#34;财产价值&#34; hibernate.hbm2ddl.auto&#34;将在数据库中生成表。