How to generate table from entities
I am currently working on a seam project using eclipse jpa tools; is it possible to 开发者_JAVA百科automatically generate sql tables from my entity definitions? If so, how do I achieve this?
It depends on the JPA implementation you are using.
With Hibernate you can specify 'create
' or 'update
' in the hibernate.hbm2ddl.auto
properties in persistence.xml
:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<persistence-unit name="yourPersistenceUnit" transaction-type="JTA">
<description>Your Persistence Unit</description>
<jta-data-source>java:/DefaultDS</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.transaction.flush_before_completion" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Possible values for hibernate.hbm2ddl.auto
property are:
create
: create database tables and indexes at startupcreate-drop
: create database tables and indexes at startup and drop at shutdownupdate
: when the application starts, check the database schema and update as needed adding missing tables and columnsvalidate
: when the application starts, check the database schema and fails if there is some missing table or column.
精彩评论