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?
In applicationContext.xml file:
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. e.g. validate | update | create | create-drop
Please check the link for more details.
You might try changing this line in your persistence.xml from
to:
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
In my case table was not created for the first time without last property listed below:
used Wildfly's in-memory H2 database
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:
try changing it to:
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!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
tocreate-drop
, because it deletes Your tables after stopping the app.