Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Automatically creating/updating the db tables based on entity classes

I have the following entity class (in Groovy):

import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue import javax.persistence.GenerationType  @Entity public class ServerNode {    @Id   @GeneratedValue(strategy = GenerationType.AUTO)   Long id    String firstName   String lastName  } 

and my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">     <persistence-unit name="NewPersistenceUnit">         <provider>org.hibernate.ejb.HibernatePersistence</provider>         <properties>             <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Icarus"/>             <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>             <property name="hibernate.connection.username" value="root"/>             <property name="hibernate.connection.password" value=""/>             <property name="hibernate.archive.autodetection" value="class"/>             <property name="hibernate.show_sql" value="true"/>             <property name="hibernate.format_sql" value="true"/>             <property name="hbm2ddl.auto" value="create"/>         </properties>         <class>net.interaxia.icarus.data.models.ServerNode</class>     </persistence-unit> </persistence> 

and the script:

import javax.persistence.EntityManager import javax.persistence.EntityManagerFactory import javax.persistence.Persistence import net.interaxia.icarus.data.models.ServerNode  def factory = Persistence.createEntityManagerFactory("NewPersistenceUnit") def manager = factory.createEntityManager()  manager.getTransaction().begin()  manager.persist new ServerNode(firstName: "Test", lastName: "Server")  manager.getTransaction().commit() 

the database Icarus exists, but currently has no tables. I would like Hibernate to automatically create and/or update the tables based on the entity classes. How would I accomplish this?

like image 478
Jason Miesionczek Avatar asked Nov 20 '08 20:11

Jason Miesionczek


People also ask

Does hibernate create tables automatically?

If the table name/field name name is as same as the table name/column name, then you don't have to specify the name attribute. Here, we specify create for the hibernate. hbm2ddl. auto property so Hibernate will create a table from the entity class.

Which property is used for auto creation of tables in hibernate?

A developer can have Hibernate create tables by adding the hbm2ddl. auto property and setting it to one of the following four values: validate. update.

What does hibernate hbm2ddl auto create drop this means?

hibernate.hbm2ddl.auto. Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop , the database schema will be dropped when the SessionFactory is closed explicitly.

What is Entity Relationship mapping in hibernate?

hibernate mappings are one of the key features of hibernate . they establish the relationship between two database tables as attributes in your model. that allows you to easily navigate the associations in your model and criteria queries.


2 Answers

I don't know if leaving hibernate off the front makes a difference.

The reference suggests it should be hibernate.hbm2ddl.auto

A value of create will create your tables at sessionFactory creation, and leave them intact.

A value of create-drop will create your tables, and then drop them when you close the sessionFactory.

Perhaps you should set the javax.persistence.Table annotation explicitly?

Hope this helps.

like image 138
toolkit Avatar answered Oct 14 '22 02:10

toolkit


You might try changing this line in your persistence.xml from

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

to:

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

This is supposed to maintain the schema to follow any changes you make to the Model each time you run the app.

Got this from JavaRanch

like image 36
billjamesdev Avatar answered Oct 14 '22 00:10

billjamesdev