Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.j256.ormlite.spring;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Set;
Spring bean that auto-creates any tables that it finds DAOs for if the property name in TableCreator.AUTO_CREATE_TABLES property has been set to true. It will also auto-drop any tables that were auto-created if the property name in TableCreator.AUTO_DROP_TABLES property has been set to true.

NOTE: If you are using the Spring type wiring in Java, initialize() should be called after all of the set methods. In Spring XML, init-method="initialize" should be used.

Here is an example of spring wiring.

 <!-- our database type factory-bean -->
 <bean id="tableCreator" class="com.j256.ormlite.db.TableCreator" init-method="initialize">
  <property name="databaseType" ref="databaseType" />
  <property name="dataSource" ref="dataSource" />
  <property name="configuredDaos">
    <ref bean="accountDao" />
    <ref bean="orderDao" />

 public class TableCreator {
 	public final static String AUTO_CREATE_TABLES = "";
 	public final static String AUTO_DROP_TABLES = "";
 	private List<BaseDaoImpl<?, ?>> configuredDaos;
If you are using the Spring type wiring, this should be called after all of the set methods.
 	public void initialize() throws Exception {
 		if (!Boolean.parseBoolean(System.getProperty())) {
 		if ( == null) {
 			throw new IllegalStateException("configuredDaos was not set in " + getClass().getSimpleName());
 		// find all of the daos and create the tables
 		for (BaseDaoImpl<?, ?> dao : ) {
 			Class<?> clazz = dao.getDataClass();
 			try {
 				DatabaseTableConfig<?> tableConfig = dao.getTableConfig();
 			} catch (Exception e) {
 				// we don't stop because the table might already exist
 				..println("Was unable to auto-create table for " + clazz);
 	public void destroy() throws Exception {
 		if (!Boolean.parseBoolean(System.getProperty())) {
 		for (DatabaseTableConfig<?> tableConfig : ) {
 			try {
 			} catch (Exception e) {
 				// we don't stop because the table might already exist
 				..println("Was unable to auto-drop table for " + tableConfig.getDataClass());
 	// not-required
 	public void setDatabaseType(DatabaseType databaseType) {
	// @Required
	public void setConnectionSource(ConnectionSource dataSource) {
		this. = dataSource;
	public void setConfiguredDaos(List<BaseDaoImpl<?, ?>> configuredDaos) {
		this. = configuredDaos;
New to GrepCode? Check out our FAQ X