Results 1 to 6 of 6

Thread: Alternative to the SchemaExportTask for hibernate / ant

  1. #1
    Join Date
    Nov 2004
    Posts
    29

    Default Alternative to the SchemaExportTask for hibernate / ant

    Hi there ,

    I am currently using hibernate and spring , and configuring my hibernate stuff through applcationContext.xml . The only thing I still need hibernate.properties is right now the SchemaExportTask in ant , which is a task that generated database tables based on my hbm.xml files .

    I was wondering if it's possible to do this from spring , and how ?

    Thanks

  2. #2
    Join Date
    Nov 2004
    Posts
    29

    Default

    is there no way for dooing this ?

    I remember seeing something on google once , but can't find anything in the docs

  3. #3
    Join Date
    Aug 2004
    Posts
    26

    Default

    I use a jdbc.properties to configure my spring DataSource bean, in my schema export I use this propreties file to generate a temporary hibernate.propreties. Hope this is of use.

    HTML Code:
    <project name="proj1" default="help" basedir=".">
    
        <property file="jdbc.properties" />
    
        <property name="hibernatePropFile"
                  value="${java.io.tmpdir}${ant.project.name}-hibernate.properties">
        </property>
    
        <target name="help">
            <echo>Hibernate Schema Generator executing from dir ${basedir}</echo>
        </target>
    
        <target name="generateHibernatePropeties">
            <echo>writing hibernate properties to: ${hibernatePropFile}</echo>
    
            <echo file="${hibernatePropFile}">
    hibernate.connection.driver_class=${jdbc.driverClassName}
    hibernate.connection.url=${jdbc.url}
    hibernate.connection.username=${jdbc.username}
    hibernate.connection.password=${jdbc.password}
    hibernate.dialect=${hibernate.dialect}
            </echo>
        </target>
    
        <fileset id="classpathSet" dir="../../lib" includes="**/*.jar" />
    
        <path id="classpath">
            <fileset refid="classpathSet" />
            <pathelement location="../../core/bin" />
            <pathelement location="../bin" />
        </path>
    
    
        <target name="schemaexport">
            <taskdef name="schemaexport"
                     classpathref="classpath"
                     classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" />
            <schemaexport properties="${hibernatePropFile}"
                          namingstrategy="com.scannellsolutions.orm.hibernate.ImprovedNamingStrategy"
                          output="${java.io.tmpdir}${ant.project.name}-schema.sql"
                          quiet="false"
                          text="true"
                          drop="no"
                          delimiter=";">
                <fileset dir="../src">
                    <include name="**/*.hbm.xml" />
                </fileset>
            </schemaexport>
        </target>
    
    </project>

  4. #4
    Join Date
    Nov 2004
    Posts
    29

    Default

    Thanks , this indeed keeps the datasource settings in a single place .

    My other question is however , how can i get spring to update the database tables without using ant at all . I've seen an example of this a good half year back , but i didn't bookmark it , copy it or pay too much attention to it at the time .

    I know it can be done all that I remember is seeing some SchemaExport like class beeing declared in the sessionFactory settings in applicationContext.xml , somewhere close to the hibernateProperties . I also think that this procedure was not wiping out the database with each run , but it was only updating it .

    Anybody know what i'm talking about ?

  5. #5
    Join Date
    Aug 2004
    Posts
    26

    Default

    to update the schema every time the application context loads just set the schemaUpdate property on your LocalSessionFactoryBean to true.

    If you only want to update it occasionally rather than every time the context loads then using a system proprety is one way to do it:

    Code:
    	<bean id="sysProps" 	class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    		<property name="targetClass" value="java.lang.System" />
    		<property name="targetMethod" value="getProperties" />
    	</bean>
    
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="mappingLocations" value="classpath*:**/*.hbm.xml" />
    		<property name="hibernateProperties">
    			<map>
    				<entry key="hibernate.dialect" value="${hibernate.dialect}" />
    			</map>
    		</property>
    		<property name="schemaUpdate">
    			<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    				<property name="targetObject" ref="sysProps" />
    				<property name="targetMethod" value="getProperty" />
    				<property name="arguments">
    					<list>
    						<value>hibernate.schemaUpdate</value>
    						<value>false</value>
    					</list>
    				</property>
    			</bean>
    		</property>
    	</bean>
    Code:
    public class SchemaUpdater {
    
        public static void main(String[] args) {
            System.setProperty("hibernate.schemaUpdate", "true");
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
                    "applicationContext.xml" });
            context.close();
        }
    
    }

  6. #6
    Join Date
    Nov 2004
    Posts
    29

    Default

    Oh I see , well thanks , I shall be giving this a try shortly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •