hibernate.hbm2ddl.auto possible values in persistence.xml


When using JPA implementation by Hibernate, pay attention to the property hibernate.hbm2ddl.auto.  The explanation is provided in this page http://www.hibernate.org/hib_docs/reference/en/html/configuration-optional.html   which means once we restart the web or application server the tables are dropped and recreated if the value is create-drop.

The other option create may not drop the tables but removes all rows in the tables.

Hence if we wish to retain the rows in the table we should specify the value as update as below.

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

For convenience, I have provided a sample persistence.xml below. The database used is MySQL.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="studentDB" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:comp/env/jdbc/DevDB</jta-data-source>
    <class>com.ts.entity.Student</class>
    <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
     </properties>
  </persistence-unit>
</persistence>

 
 
 
 
Comments:

Post a Comment:
  • HTML Syntax: Allowed
 

« January 2009
SunMonTueWedThuFriSat
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
       
Today

Valid XHTML or CSS?

[This is a Roller site]
 
© Karthik