Hibernate: Automatically creating/updating the db tables based on entity classes
Solution 1
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.
Solution 2
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
Solution 3
Sometimes depending on how the configuration is set, the long form and the short form of the property tag can also make the difference.
e.g. if you have it like:
<property name="hibernate.hbm2ddl.auto" value="create"/>
try changing it to:
<property name="hibernate.hbm2ddl.auto">create</property>
Solution 4
In my case table was not created for the first time without last property listed below:
<properties>
<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-drop"/>
<!-- without below table was not created -->
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
</properties>
used Wildfly's in-memory H2 database
Solution 5
There is one very important detail, than can possibly stop your hibernate from generating tables (assuming You already have set the hibernate.hbm2ddl.auto
). You will also need the @Table
annotation!
@Entity
@Table(name = "test_entity")
public class TestEntity {
}
It has already helped in my case at least 3 times - still cannot remember it ;)
PS. Read the hibernate docs - in most cases You will probably not want to set hibernate.hbm2ddl.auto
to create-drop
, because it deletes Your tables after stopping the app.
Related videos on Youtube
![Jason Miesionczek](https://i.stack.imgur.com/lKpl6.jpg?s=256&g=1)
Jason Miesionczek
I am a PHP dev at my day job, but in my spare time i like to play around with all sorts of different technologies: Go/Rust Java/Scala/Kotlin/Android C/C++
Updated on September 08, 2021Comments
-
Jason Miesionczek almost 3 years
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?
-
Jason Miesionczek over 15 yearsIt was the missing 'hibernate' at the beginning of the hbm2dll.auto. thanks!
-
sguan over 8 yearsI just removed that line and it won't drop the table. Hope this helps!
-
Aman Nagarkoti over 7 yearshow can I make hibernate to create tables only if they didn't exists?
-
Jelaby almost 3 yearsThis solution has already been proposed in other answers to this question.