Jacob
Jacob

Reputation: 14731

HIbernate creating database table

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?

Upvotes: 2

Views: 5446

Answers (4)

Matt
Matt

Reputation: 11805

Set it to "none" in a production environment.

Upvotes: 1

Reimeus
Reimeus

Reputation: 159784

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(!)

Upvotes: 2

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

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?

Upvotes: 1

Luis Ramirez-Monterosa
Luis Ramirez-Monterosa

Reputation: 2242

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)

Upvotes: 4

Related Questions