Soft8 Stev
Soft8 Stev

Reputation: 63

hibernate change data structure

i am new in hibernate. i just learn it from hibernate tuto. i am glad that i don't have to type manualy the class for CRUD operation. (i am from .NET side). i think it is like Entity Framework on .NET side.

but then i think, what happen when i change the structure, or data type on my database ? when i forgot another table after generation, how can i regenerate it again ?

on .NET world, i just have to delete the table and put it again after modification and regenerating the solution.

how can i do that on J2EE world ?

Upvotes: 1

Views: 65

Answers (1)

Milkmaid
Milkmaid

Reputation: 1754

You are right. It is ORM therefore mostly the same as entity framework. If you want to generate or change your database structure from your entities you can do it this way:

In your persistence.xml or hibernate.properties add line hibernate.hbm2ddl.auto

in persistence.xml:

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

you can use one of these values:

  • create-only

    Database creation will be generated.

  • drop

    Database dropping will be generated.

  • create

    Database dropping will be generated followed by database creation.

  • create-drop

    Drop the schema and recreate it on SessionFactory startup. Additionally, drop the schema on SessionFactory shutdown.

  • validate

    Validate the database schema

  • update

    Update the database schema

You can read more about setup Here

Use this way to change your schema in productive environment is strongly not recommended (However, you can let Hibernate to generate the changes into the script)

Upvotes: 1

Related Questions