我有一個hibernate項目,我想自動創建表。如果已經創建了一個表,那麼我會在Entity類中添加一個新字段,然後我想在表格中創建一個新字段而不刪除表格的數據。我將在下面給出我的源代碼。我無法從休眠項目自動創建表
的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="Inventory" package="com.mycompany.testhibernate"/>
</session-factory>
</hibernate-configuration>
EntityClass
package com.mycompany.testhibernate;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Inventory implements Serializable {
private Integer id;
private String itemName;
private String itemNote;
private Integer quantity;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getItemName() {
return this.itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemNote() {
return itemNote;
}
public void setItemNote(String itemNote) {
this.itemNote = itemNote;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public int getQuantity() {
return this.quantity;
}
}
做u使用哪個版本的MySQL? –
mysql版本5.6 – Riyad