How to generate default values with JPA hibernate [closed]

2

I would like to optimize my code and my work with the following situation. I have an entity address that has one type, that is, another entity called TpEndereco. Since this table has default values, for example: commercial, other, residential ...

I would like to know if there is a way to initialize these values when starting the application and still be able to insert new records in it.

    
asked by anonymous 07.01.2016 / 19:18

1 answer

2

Until the last time I checked, there was nothing in the JPA specification about data load, that is, some default to insert values into tables automatically.

Specifically, Hibernate has a setting hibernate.hbm2ddl.import_files that allows you to specify SQL files that will be executed when the database is created. Note that this will only work if the database creation type is create or create-drop , in the hibernate.hbm2ddl.auto property.

However, I would not recommend using this function for several reasons:

  • Content is specific to each database
  • Insert structure may look different from your classes
  • This only works on bank creation, can not update or add values

For production, a more specialized solution is better. The most basic would be to develop a routine that is executed at system startup and inserts values if they do not exist.

Otherwise there are several more advanced alternatives for versioning databases. Some examples:

08.01.2016 / 01:51