0

I have a hibernate project and I want to auto create table. And if a table is already created then I add a new field in the Entity class then I want to create a new field in the table without deleting data of the table.I'm giving my source in the below.

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


   }
2
  • which mysql version do u used? Commented Nov 16, 2016 at 9:55
  • mysql version 5.6 Commented Nov 16, 2016 at 10:41

1 Answer 1

1

Did you try with this option?

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

Should update your DDL according to your beans

hbm2ddl.auto update :

If the value is update then hibernate checks for the table and columns. If table doesn’t exist then it creates a new table and if a column doesn’t exist it creates new column for it.

Also change the property to:

<mapping class="com.mycompany.testhibernate.Inventory"/>
Sign up to request clarification or add additional context in comments.

6 Comments

I also tried by <property name="hibernate.hbm2ddl.auto">update</property> but it does not work
I guess your mapping tag is wrong, should be <mapping class="com.mycompany.testhibernate.Inventory"/>
public static void main( String[] args ) { AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(Inventory.class ); config.configure(); new SchemaExport(config).create(true, true); } When I run the following code from entity class I can create table in the database and delete the existing data from the table.but I do not want this.
Use SchemaUpdate update = new SchemaUpdate( configuration ); update.execute(...)
thanks to cralfaro,but your code will be : new SchemaUpdate(config).execute(true, true) excepting of new SchemaExport(config).create(true, true);;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.