-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Writting database update script to an external file
PostPosted: Tue Nov 08, 2005 3:56 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 3:18 am
Posts: 32
Hi,
I'm using hibernate 3.1 with annotations and spring framework.
I would like to configure hibernate so that the database update script (such altering or creating tables) is automatically written to an external file (whenever I make an update to my objects).

I found some functions such as generateSchemaUpdateScript() but I don't know how to use it.
I don't want to use hibernate tools.
Could anyone help me in that. If anyone could provide me with an example I would be very thankful.

Thanks in advance.[/u]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 6:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
may I ask why you dont want to use hibernate tools or the builtin schemaexport ant task ?

In any case look at the src code for the schemaupdatetask and you can see how it call it to dump the stuff in a external file.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 9:49 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 3:18 am
Posts: 32
Hi max,
I don't want to use hibernate tools because I've been trying to use it for a long time and it causes errors.
Here is a sample of the ant task code I tried to use:
Code:
<path id="hibernateTool.classpath">
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="hibernate3.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="hibernate-tools*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="mysql-connector-java*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="velocity*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="jtidy*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="commons*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="hibernate-annotations.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="dom4j*.jar"/>
   </path>
         
   <target name="hibernatetool">
          <taskdef name="hibernatetool"
              classname="org.hibernate.tool.ant.HibernateToolTask"
              classpathref="hibernateTool.classpath"/>
         
      <hibernatetool destdir="${tomcat.home}/webapps/4s/ant">
         
         <annotationconfiguration />
         
         <hbm2ddl update="true"
           outputfilename="schema.sql"
           delimiter = ";"/>
      </hibernatetool>
      </target>


Here is the error I get:

Quote:
Problems in creating a AnnotationConfiguration. Have you remembered to add it to the classpath ?

Although it is added to claspath.

Plus, I'm looking for something that is called automaticaly. I don't want to run an ant task every time I make a modification [b][u](if this is possible)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 9:55 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 3:18 am
Posts: 32
sherihan wrote:
Hi max,
I don't want to use hibernate tools because I've been trying to use it for a long time and it causes errors.
Here is a sample of the ant task code I tried to use:
Code:
<path id="hibernateTool.classpath">
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="hibernate3.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="hibernate-tools*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="mysql-connector-java*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="velocity*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="jtidy*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="commons*.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="hibernate-annotations.jar"/>
      <fileset dir="${tomcat.home}/webapps/4s/WEB-INF/lib" includes="dom4j*.jar"/>
   </path>
         
   <target name="hibernatetool">
          <taskdef name="hibernatetool"
              classname="org.hibernate.tool.ant.HibernateToolTask"
              classpathref="hibernateTool.classpath"/>
         
      <hibernatetool destdir="${tomcat.home}/webapps/4s/ant">
         
         <annotationconfiguration />
         
         <hbm2ddl update="true"
           outputfilename="schema.sql"
           delimiter = ";"/>
      </hibernatetool>
      </target>


Here is the error I get:

Quote:
Problems in creating a AnnotationConfiguration. Have you remembered to add it to the classpath ?

Although it is added to claspath.

Plus, I'm looking for something that is called automaticaly. I don't want to run an ant task every time I make a modification [b][u](if this is possible)


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 2:06 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I haven't seen you ask for solutions to your problem - so if you showed the full stacktrace it might help. Run ant -v to get it.


And what do you mean by run automatically that is not ant based ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 2:10 pm 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Perhaps if your problem is with the <annotationconfiguration/> portion of the tool, and not the <hbm2ddl/> portion, you should remove the reference to <annotationconfiguration/>?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 3:18 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 3:18 am
Posts: 32
Hi max,
I haven't asked for a solution to my problem on this forum but I posted many posts on springframework forum and non of the replies I got solved my problem.
Any way, if you could provide me with a solution I would be very thankful.
Note:
- I'm using annotations and my hibernate configuration file is a .xml file not a .cfg file.(I'm not sure if this differs).

Regarding what I mean by automatically,
As i mentioned before I'm using spring framework. I have a file hibernate.xml that contains hibernate configuration.
In this file I configure a bean named session factory. Here is the code:

Code:
<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource">
         <ref bean="dataSource" />
      </property>
      <property name="configurationClass">
         <value>org.hibernate.cfg.AnnotationConfiguration</value>
      </property>
      <property name="entityInterceptor">
         <ref bean="auditLogInterceptor" />
      </property>

      <property name="mappingDirectoryLocations">
         <list>
            
         </list>
      </property>

      <property name="annotatedClasses">
         <list>
            <!-- Here I add the annotated classes -->
            <value>com._4s_.banks.model.Bank</value>
            <value>com._4s_.banks.model.accounts</value>
            
                                                                          
         </list>
      </property>

      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">
               ${hibernate.dialect}
            </prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <!--<prop key="hibernate.hbm2ddl.auto">create</prop> -->
         </props>
      </property>
      
   
   </bean>



If you have a look at the property named hibernateProperties, you'll find a <prop> named "hibernate.hbm2dll.auto" and it takes a value "update" .
This causes the database to be updated with the new modifications whenever the application is reloaded.
That's what I'm looking for, I want these updates to be written to an external file whenever the application is reloaded as they are excuted in the database.

Code:
<property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">
               ${hibernate.dialect}
            </prop>
            <prop key="hibernate.show_sql">false</prop>
            [b]<prop key="hibernate.hbm2ddl.auto">update</prop>[/b]

         </props>
      </property>


Hope I'm clear now.
Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 4:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you want the schemaupdate to dump *just* to an external file and not run it against the database ?

For that the ant tasks makes *much* more sense - why would you like to launch the whole container etc. just to generate a "diff" ?

If you still want that you will need to call schemaupdate directly with a filename to make it export - you can see examples of that in the sourcecode.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 6:15 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 3:18 am
Posts: 32
Hi,
It seems that I can't explain what I need clearly.
I don't want to dump to an external file without running it against the datebase.
On the contrary, I want both.
I want the updates to be excuted against the database and to be written to an external file at the same time.

I can't undersand what do u mean by the following statment.
Quote:
why would you like to launch the whole container etc. just to generate a "diff" ?


Quote:
If you still want that you will need to call schemaupdate directly with a filename to make it export - you can see examples of that in the sourcecode.

Would you please tell me where exactly could I find the examples you mentioned.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 6:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you still then need to run the schemaupdate "your own way"

And I already pointed you to where you could find the way to do it.

Look in SchemaUpdate and the related ant tasks.

Use a decent IDE to find references to it and you should find the relevant code in no time.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.