/**
Author: willpower
Source: rifoo Technology (http://www.rifoo.com)
Date: 2006-07
Remarks: Reprinted please keep the above statement
**/
Today, we will continue to learn Chapter 3 to create a simple application. The previous article talked about creating a hibernate configuration file, and we created a configuration file in XML format. Of course, as we know in the previous study, Hibernate also provides another property file method to store configuration information.
Create a common Java property file with the following content:
Code:Hibernate. Connection. driver_class = org. HSQLDB. jdbcdriver
Hibernate. Connection. url = JDBC: HSQLDB: hsql: // localhost/hibernate
Hibernate. Connection. Username = sa
Hibernate. Connection. Password =
Hibernate. pool_size = 5
Hibernate. show_ SQL = false
Hibernate. dialect = org. hibernate. dialect. hsqldialect [copy to clipboard]
We have noticed that it does not contain XML ing resources. In fact, we cannot include such ing information in the property file. We need to do this ing in the configuration class. The following is a simple list:
Code:Configuration Config = new configuration ();
Config. addclass (motd. Class );
Config. setproperties (system. getproperties ());
Sessionfactory sessions = config. buildsessionfactory (); [copy to clipboard]
Note: The configuration object searches for the ing file in classpath, and searches for the ing file in the package with the same name as the class. Because the full name of our class is book. hibernate. gettingstarted. motd, we can see the following pairs of files in the root path of classpath, a class and an hbm xml ing file, as shown below:
/Book/hibernate/gettingstarted/motd. Class
/Book/hibernate/gettingstarted/motd. HBM. xml
Of course, if we want to manually change the directory of the configuration file, we need to add them as resources to the configuration object, as shown below:
Code:Configuration Config = new configuration ();
Config. addresource ("config/motd. HBM. xml ");
Config. setproperties (system. getproperties ());
Sessionfactory sessions = config. buildsessionfactory (); [copy to clipboard]
There may be more configuration files in the instance. However, there is a convenient way to manage the configuration files. That is, we put the class and its ing files in the same directory, and their commands should be similar or identical, such as motd. HBM. XML files are mapped to the motd class, all of which are under the same package (directory. This method allows us to quickly find any desired class ing and maintain the readability of the ing file.
If you do not want to use a file to provide configuration information, the third method is to directly use the-D flag in the command line.For example:
Code:Java-classpath...
-Dhibernate. Connection. driver_class = org. HSQLDB. jdbcdriver
-Dhibernate. Connection. url = JDBC: HSQLDB: hsql: // localhost/hibernate
-Dhib.pdf. Connection. Username = sa
-Dhib.pdf. Connection. Password =
-Dhib.pdf. pool_size = 5
-Dhib.pdf. show_ SQL = false
-Dhibect. dialect = org. hibernate. dialect. hsqldialect
Org. hibernate. tool. hbm2ddl. schemaexport
-- Output = advert. SQL
/Book/hibernate/gettingstarted/motd. HBM. xml [copy to clipboard]
This method may be the most direct among the three methods, which is sometimes useful.
However, in most cases, we strongly recommend using configuration files in XML format, which is the best choice.