In hibernate, whenever I change the Entity configuration, do I have to drop the table in the database?

5

Example: I have the Person class below:

@Entity
public class Pessoa {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idPessoa;
    private String nome;

    //getters e setters

}

Hibernate, by default, persists in the database a String as Varchar, and I want to persist in the database as text and change to the following configuration:

@Entity
public class Pessoa {

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

    @Column(columnDefinition = "text")
    private String nome;

    //getters e setters

}

Adding the annotation "@Column (columnDefinition=" text ")" into the String attribute, and for this to persist as text, I am dropping the table already created in the database, and my question is:

Would there be a way for me not to drop the table in the database, since in a more complex system, can this cause me consistency problems?

    
asked by anonymous 08.08.2015 / 15:41

1 answer

2

Correct, Because when you let hibernate generate the tables for you based on your templates, it just creates things, not deletes them.

Unless you make the following configuration:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />
  • But it will delete the entire database and create it again every time you start your application server.

The best way is to make this process at hand, as it is not advisable for projects running in production to let hibernate do maintenance on your database.

Ideally, you have a structure where you can check what is different in the database, and just show the scripts you should apply to, such as creating tables and adding fields. And the rest like renaming tables or even deleting a field or table you do the same script in the hand.

    
14.09.2015 / 20:20