2

I am in the process of learning JPA - Hibernate.

I am following this article In Dog.java it is mentioned as @Table(name = "dog"). In persistence.xml I have the following

<property name="hibernate.hbm2ddl.auto" value="create"/>

Does this creates table dog in database? I have not created table Dog in database. So in production environment this could be dangerous though. In such scenarios what should be ideal value for hibernate.hbm2ddl.auto?

Any suggestions?

catch23
  • 17,519
  • 42
  • 144
  • 217
Jacob
  • 14,463
  • 65
  • 207
  • 320

4 Answers4

4

it's dangerous in all senses, your application user should not have DDL permissions (alter table, create tables) your application user should only do DML (SELECT, INSERT,UPDATE,DELETE, etc)

  • If application user should do only DML, then what should be the value for `hibernate.hbm2ddl.auto`? Is it `none`? Thanks – Jacob Aug 20 '12 at 19:01
  • just don't add it, comment it out. Reason behind is someone can manipulate that connection in order to drop a table or something. more secure if you don't manipulate with same user. – Luis Ramirez-Monterosa Aug 20 '12 at 19:06
2

Yes, it does create the new table every time that your app is deployed. Better to use:

<property name="hibernate.hbm2ddl.auto" value="validate"/>

if you already have data in place.

Here are the possible options:

  • validate: validate the schema, makes no changes to the database.
  • update: update the schema.
  • create: creates the schema, destroying previous data(!)
  • create-drop: drop the schema at the end of the session(!)
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

Do not set <property name="hibernate.hbm2ddl.auto" value="create"/> in production, because whenever you restart the server, all tables will be deleted and newly created again. You can make use of this property(hibernate feature) if you are migrating from one database to another.

If you want to set then set <property name="hibernate.hbm2ddl.auto" value="update"/> in development(not in production). This will update the schema if there are any changes you have made in pojo classes(annotations).

Also check : Hibernate: hbm2ddl.auto=update in production?

Hibernate hbm2ddl.auto possible values and what they do?

Community
  • 1
  • 1
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
1

Set it to "none" in a production environment.

Matt
  • 11,523
  • 2
  • 23
  • 33