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.spring.TableCreator" init-method="initialize">
  <property name="connectionSource" ref="connectionSource" />
  <property name="configuredDaos">
   <list>
    <ref bean="accountDao" />
    <ref bean="orderDao" />
   </list>
  </property>
 </bean>
 

Author(s):
graywatson
 
 public class TableCreator {
 
 	public final static String AUTO_CREATE_TABLES = "ormlite.auto.create.tables";
 	public final static String AUTO_DROP_TABLES = "ormlite.auto.drop.tables";
 
 	private List<Dao<?, ?>> configuredDaos;
 
 	public TableCreator() {
 		// for spring
 	}
 
 	public TableCreator(ConnectionSource connectionSourceList<Dao<?, ?>> configuredDaos) {
 		this. = connectionSource;
 		this. = configuredDaos;
 	}

Possibly create the tables is the AUTO_CREATE_TABLES system property is set to "true".
 
 	public void maybeCreateTables() throws SQLException {
 	}

If you are using the Spring type wiring, this should be called after all of the set methods.
 
 	public void initialize() throws SQLException {
 		if (!Boolean.parseBoolean(System.getProperty())) {
 			return;
 		}
 
 		if ( == null) {
 			throw new SQLException("configuredDaos was not set in " + getClass().getSimpleName());
 		}
 
 		// find all of the daos and create the tables
 		for (Dao<?, ?> dao : ) {
 			Class<?> clazz = dao.getDataClass();
 			try {
 				DatabaseTableConfig<?> tableConfig = null;
 				if (dao instanceof BaseDaoImpl) {
 					tableConfig = ((BaseDaoImpl<?, ?>) dao).getTableConfig();
 				}
 				if (tableConfig == null) {
 					tableConfig = DatabaseTableConfig.fromClass(clazz);
 				}
 				TableUtils.createTable(tableConfig);
 				.add(tableConfig);
 			} catch (Exception e) {
 				// we don't stop because the table might already exist
 				..println("Was unable to auto-create table for " + clazz);
			}
		}
	}

Possibly drop the tables that were previously created if the AUTO_DROP_TABLES system property is set to "true".
	public void maybeDropTables() throws SQLException {
	}
	public void destroy() throws SQLException {
		if (!Boolean.parseBoolean(System.getProperty())) {
			return;
		}
		for (DatabaseTableConfig<?> tableConfig : ) {
			try {
				TableUtils.dropTable(tableConfigfalse);
catch (Exception e) {
				// we don't stop because the table might already exist
				..println("Was unable to auto-drop table for " + tableConfig.getDataClass());
			}
		}
	}
	// This is @Required
	public void setConnectionSource(ConnectionSource dataSource) {
		this. = dataSource;
	}
	public void setConfiguredDaos(List<Dao<?, ?>> configuredDaos) {
		this. = configuredDaos;
	}
New to GrepCode? Check out our FAQ X