2016-11-16 23 views
0

私は休止状態のプロジェクトがあり、テーブルを自動作成したいと思います。テーブルがすでに作成されている場合は、Entityクラスに新しいフィールドを追加してから、テーブルのデータを削除せずにテーブルに新しいフィールドを作成したいと考えています。hibernateプロジェクトからテーブルを自動作成できません

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


    } 
+0

Uは、MySQLのバージョンを使用していますか? –

+0

mysqlバージョン5.6 – Riyad

答えて

1

あなたは、このオプションを使用してみてくださいましたか?値が更新され

場合は、テーブルと の列のチェックを休止:

<property name="hibernate.hbm2ddl.auto">update</property> 

hbm2ddl.auto更新あなたの豆に応じて、あなたのDDLを更新する必要があります。テーブルが存在しない場合は新しいテーブルが作成され、 カラムが存在しない場合は新しいカラムが作成されます。

またにプロパティを変更します。

<mapping class="com.mycompany.testhibernate.Inventory"/> 
+0

私は<プロパティ名= "hibernate.hbm2ddl.auto">アップデートでも試してみましたが、動作しません。 – Riyad

+0

あなたのマッピングタグが間違っていると思いますので、 cralfaro

+0

public static void main(String [] args){ AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(Inventory.class); config.configure(); 新しいSchemaExport(config).create(true、true); }エンティティクラスから次のコードを実行すると、データベースにテーブルを作成し、既存のデータをテーブルから削除することができますが、これは必要ありません。 – Riyad

関連する問題