In Hibernate, whenever I change the Entity configuration, do I have to drop the tables in the database?

Asked

Viewed 374 times

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 bank a String like 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")" in the String attribute, and for that to persist as text, I am dropping the table already created in the database, and my question is as follows:

There would be a way I don’t need to drop the table in the database, since in a more complex system, this can cause me consistency problems?

  • I worked on a project that when I had these changes we changed the class and gave a alter in the database column. I don’t know if it’s the best way, but it worked. That’s because there were times when Hibernate didn’t update all tables running with update or stop creating some kind of Quence.

  • @Maiconcarraro I wanted to eliminate this alter also, I wanted to be unconcerned with the bank, so that I could enter the bank as little as possible. Even because there are customers who don’t like it very much when we ask for access to the bank

  • http://stackoverflow.com/questions/12400825/jpa-data-too-long-for-column-does-not-change

  • then @Maiconcarraro my xml ta similar to the tutorial there, and nothing, not yet modified, to have to modify by the database yet

  • The tutorial he’s just simulating, So in essence the create ( which first drops the table and then recreates) works correctly however the update does not if there is a modification in property metadata. In the last answer there is another solution, but I do not know if it works.

  • 1

    This type of change is always boring to manage even. I particularly use the flywaydb to manage all DDL for me, I don’t use JPA Provider for this, in it I can manage everything, do roolback of amendments, etc..

  • @Brunocésar as uses this flywaydb?

  • @Tiagoferezin he works on the concept of migrates. You can use pure SQL or java even, describing the migrates, to versioning the base schema. See documentation the idea of it, the articles and such. If it really suits you, I can include a full example of its use.

  • @Brunocésar can put as an answer an example, that if it works I put as accepted, but what I really wanted, is something in Hibernate same. if possible

  • 1

    The feature of Hibernate to create or change the database as annotations in the entities is only suitable to use in production bases if the system is really very simple and the integrity of the data is not even so important. Most systems aren’t like that. You will probably have to develop or choose a separate process and tool to migrate the database instead of Hibernate for this.

Show 5 more comments

1 answer

2


Correct, Because when you let Hibernate generate the tables for you based on your templates, it just creates things, and doesn’t delete.

Unless you make the following configuration:

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

The best way is to do this process by hand, as it is not advisable in projects running in production to let Hibernate do the maintenance in your database.

The ideal is you a structure in which it checks what’s different in the database and just show you the scripts you should apply, such as creating tables and adding fields. And the rest as changing table names or even deleting a field or table you make the script in the same hand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.