Results 1 to 5 of 5
  1. #1
    bbq
    bbq is offline Senior Member
    Join Date
    Jun 2007
    Posts
    132
    Rep Power
    0

    Default variables in hibernate.cfg.xml file

    Does anybody know how can I refer a variable from hibernate.cfg.xml?

    Java Code:
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    <session-factory>
    <property name="show_sql">$value</property>
    <property name="hibernate.connection.username">$value</property> 
    <property name="hibernate.dialect">$value</property>
    <property name="hibernate.jdbc.batch_size">$value</property>
    </session-factory>
    </hibernate-configuration>

  2. #2
    Peter is offline Senior Member
    Join Date
    Jun 2007
    Posts
    119
    Rep Power
    0

    Default

    You can construct the hibernate.cfg file at execution time, and then you create the procedure that you send as parameter an array or list
    It would be something like that
    Java Code:
     public static void buildOtherConfiguration(String  args[]) { 
          try {
             Configuration cfg = new Configuration();
           
             cfg.setProperty("hibernate.connection.driver_class","com.microsoft.jdbc.sqlserver.SQLServerDriver ");
             cfg.setProperty("hibernate.connection.url",args[0]);
             cfg.setProperty("hibernate.connection.databasename",args[1]);
             cfg.setProperty("hibernate.connection.username ",args[2]);
             cfg.setProperty("hibernate.connection.password",args[3]);
             cfg.setProperty("dialect",args[4]);
             cfg.setProperty(Environment.DIALECT,args[5]);
             cfg.setProperty("transaction.factory_class","org.hibernate.transaction.JDBCTransactionFactory");
             cfg.setProperty("hibernate.cache.provider_class","org.hibernate.cache.HashtableCacheProvider ");
    
             
            
             cfg.addResource("model/ventas/entity/Client.hbm.xml");
             cfg.addResource("model/ventas/entity/Sell.hbm.xml"); 
        
          } catch (Throwable ex) {
             ex.printStackTrace();
          }
       }

  3. #3
    shalini is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default

    I want to use variable in the password tag in hibernate.cfg.xml file.please help me .

  4. #4
    shalini is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default

    Not getting you....where I wll call the procedure..?

  5. #5
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    You can do this with ANT

    build.xml
    Java Code:
    <?xml version="1.0"?>
    <project default="setup-hibernate.cfg.xml" name="TEST" basedir=".">
    	<property file="${user.home}/build.properties" />
    	<loadproperties srcfile="build.properties" />
    
    	<target name="prompt">
    		<input message="Database: " addproperty="test.input.dialect" defaultvalue="${test.default.dialect}" validargs="${test.build.dialect.options}" />
    	</target>
    
    	<target name="set_conditions">
    		<condition property="test.input.dialect" value="${test.default.dialect}">
    			<not>
    				<isset property="test.input.dialect" />
    			</not>
    		</condition>
    		<condition property="hibernate.dialect" value="${test.hibernate.dialect.db2}">
    			<equals arg1="${test.input.dialect}" arg2="db2" casesensitive="false" />
    		</condition>
    		<condition property="hibernate.dialect" value="${test.hibernate.dialect.sqlserver}">
    			<equals arg1="${test.input.dialect}" arg2="sqlserver" casesensitive="false" />
    		</condition>
    		<condition property="hibernate.dialect" value="${test.hibernate.dialect.oracle}">
    			<equals arg1="${test.input.dialect}" arg2="oracle" casesensitive="false" />
    		</condition>
    	</target>
    
    	<target name="setup-hibernate.cfg.xml" depends="prompt, set_conditions">
    		<delete dir="build/temp" />
    		<filter token="hibernateDialect" value="${hibernate.dialect}" />
    		<mkdir dir="build/temp" />
    		<copy todir="build/temp" filtering="true">
    			<fileset dir=".">
    				<include name="hibernate.cfg.xml" />
    			</fileset>
    		</copy>
    	</target>
    </project>
    build.properties
    Java Code:
    # Default Dialect
    test.default.dialect=db2
    
    # Available Dialects
    test.build.dialect.options=db2,sqlserver,oracle
    
    # Dialect Replacement Strings
    test.hibernate.dialect.sqlserver=org.hibernate.dialect.SQLServerDialect
    test.hibernate.dialect.db2=org.hibernate.dialect.DB2Dialect
    test.hibernate.dialect.oracle=org.hibernate.dialect.Oracle9Dialect
    hibernate.cfg.xml
    Java Code:
    <?xml version="1.0" encoding="utf-8"?>
    <hibernate-configuration>
    	<session-factory name="TestSessionFactory">
    		<property name="dialect">@hibernateDialect@</property>
    	</session-factory>
    </hibernate-configuration>
    resulting hibernate.cfg.xml if you run the default target and select sqlserver as the input dialect
    Java Code:
     <?xml version="1.0" encoding="utf-8"?>
    
    <hibernate-configuration>
    	<session-factory name="TestSessionFactory">
    		<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    	</session-factory>
    </hibernate-configuration>

Similar Threads

  1. boolean variables
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-31-2007, 04:58 AM
  2. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM
  3. building file and variable names from variables
    By madad2005 in forum New To Java
    Replies: 2
    Last Post: 07-18-2007, 04:47 PM
  4. Replies: 2
    Last Post: 06-30-2007, 03:01 PM

Posting Permissions

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