How to generate default values with JPA Hibernate

Asked

Viewed 189 times

2

I would like to optimize my code and my work with the following situation. I have an address entity that has a type, ie another entity called Tpendereco. Since, this table has standard values, example: commercial, other, residential...

I wonder if there is a way to initialize these values when starting the application and still be able to insert new record in it.

1 answer

2


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

Specifically Hibernate has a configuration hibernate.hbm2ddl.import_files which allows specifying SQL files that will be executed at the time of database creation. Note that this will only work if the bank creation type is create or create-drop, on the property hibernate.hbm2ddl.auto.

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

  • Content is specific to each database
  • The structure of the Inserts may differ from its classes
  • This only works when creating the database, there is no way to update or add values

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

Other than that, there are several more advanced alternatives to using databases. A few examples:

  • thanks for the help! Is that I saw in the Demosielle Framework there was this option, I thought there might be something native in JPA. But thanks for the clarification.

Browser other questions tagged

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