[Original] Pro hibernate 3 notes and summary (7) Chapter 3 creating simple applications

Source: Internet
Author: User
/**
Author: willpower
Source: rifoo Technology (http://www.rifoo.com)
Date: 2006-07
Remarks: Reprinted please keep the above statement
**/

This chapter begins with a more detailed application. Please study the content of this book with me. Our example name is "message of the Day". In this chapter, we can simply call it "motd. It is a UNIX program that can send some current notifications when a user logs on to the system.

Installation Tools

Suppose you have installed JDK or later, you also need the support of the hibernate package and a database. Of course, you can also install an ant build tool.

Hibernate3

Download the latest version from the official website and decompress it to any local directory. The example in this book contains the directory c:/home/hibernate-3.0, the decompressed file includes the hibernate source file and jar package, as well as the third-party library lib required by hibernate.

Hsql 1.7.3.3

In this example, the database uses hsql, which is an open-source database written in Java. You can download the latest program at http://hsqldb.sourceforge.net. Download the package and decompress it to any local directory. In this example, the directory is C:/home/HSQLDB /. Next we will use a database named hibernate. First, create a directory containing the hibernate file, such as C:/home/HSQLDB/hibernate /. Create a new server. properties file and put it in this directory.

Listing 3-1. configuration file of the hibernate Sample Database

Code:# Filename: C:/home/HSQLDB/server. Properties
#
# Hibernate examples database-create
# Database on the default port.
# Specifies the path to the database
# Files-note that the trailing slash
# Is required.
Server. database.0 = file:/home/HSQLDB/hibernate/
# Specifies the name of the database
Server. dbname.0 = hibernate [copy to clipboard]

Manually enter the following command in the command line to start the database:
Java-classpath ../lib/HSQLDB. Jar org. HSQLDB. Server

To close the database, you can directly close the command line window, but if you want to close it in another elegant way, you need to write a simple Java program (HSQLDB is required. jar can be found in classpath)

Listing 3-2. Simple shutdown logic for the hsql ServerCode:Import java. SQL .*;
Public class shutdown {
Public static void main (string [] argv) throws exception {
Class. forname ("org. HSQLDB. jdbcdriver ");
Connection c = drivermanager. getconnection (
"JDBC: HSQLDB: hsql: // localhost/hibernate ",
"Sa ",
"");
Statement S = C. createstatement ();
S.exe cute ("shutdown ");
C. Close ();
}
} [Copy to clipboard]

In actual situations, to connect to different databases, we only need to change the following content:
• Dialect)
• JDBC driver
• Database Connection URL
• Database username
• Database Password

These will be discussed in later chapters.

Ant 1.6.2

Ant is optional. Of course we recommend that you install ant because it can easily build a Java application. You can download the latest installer at http://ant.apache.org/on the ant homepage. We assume that you are familiar with ant, so we will not explain the detailed meaning of the build. xml file later. The example in this book is constructed using ant1.6.2.
The following list provides the ant script for building this example,Note: Our ant task contains a schema task, which uses the hibernate tool to directly generate a database schema from the mapping file, that is, to automatically create a database.

Listing 3-3. Construct the ant script of Chapter 3 sampleCode:<Project default = "all">
<Property name = "hibernate" location = "/home/hibernate-3.0"/>
<Property name = "JDBC" location = "/home/HSQLDB. Jar"/>
<Property name = "src" location = "src"/>
<Property name = "Config" location = "."/>
<Property name = "Dist" location = "Dist"/>
<Property name = "bin" location = "$ {Dist}/bin"/>
<Property name = "lib" location = "$ {Dist}/lib"/>
<Property name = "name" value = "chapter03"/>

<Path id = "classpath. Base">
<Pathelement location = "."/>
<Pathelement location = "$ {bin}"/>
<Pathelement location = "$ {hibernate}/hibernate3.jar"/>
<Fileset dir = "$ {hibernate}/lib" includes = "**/*. Jar"/>
<Pathelement location = "$ {JDBC}"/>
</Path>

<Target name = "init">
<Mkdir dir = "$ {Dist}"/>
<Mkdir dir = "$ {bin}"/>
<Mkdir dir = "$ {lib}"/>
</Target>

<Target name = "schema">
<Taskdef
Name = "schemaexport"
Classname = "org. hibernate. tool. hbm2ddl. schemaexporttask"
Classpathref = "classpath. Base"/>

<Schemaexport
Properties = "hibernate. properties"
Quiet = "no"
TEXT = "yes"
Drop = "no"
Output = "export. SQL">
<Fileset dir = "$ {SRC}">
<Include name = "**/*. HBM. xml"/>
</Fileset>
</Schemaexport>

</Target>

<Target name = "compile" depends = "init">
<Javac srcdir = "$ {SRC}" destdir = "$ {bin}">
<Classpath refID = "classpath. Base"/>
</Javac>
</Target>

<Target name = "Dist" depends = "compile">
<Jar
Destfile = "$ {lib}/$ {name}. Jar"
Basedir = "."
Includes = "build. XML, src/**, CFG/**, bin /**"
Excludes = ". classpath,. Project,. cvsignore"/>
</Target>

<Target name = "clean">
<Delete dir = "$ {Dist}"/>
<Delete dir = "$ {bin}"/>
<Delete file = "$ {lib}/$ {name}. Jar"/>
</Target>

<Target name = "all" depends = "Dist"/>

</Project> [copy to clipboard]

Hibernate configuration file

Hibernate needs to know the database it connects to and some related ing methods.

Listing 3-4. ing file of the "message of the Day" ApplicationCode:<? XML version = '1. 0' encoding = 'utf-8'?>
<! Doctype hibernate-Configuration
Public "-// hibernate/hibernate configuration DTD // en"
"[Url] http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd#/url]">

<Hibernate-configuration>
<Session-factory>
<Property name = "connection. driver_class">
Org. HSQLDB. jdbcdriver
</Property>
<Property name = "connection. url">
JDBC: HSQLDB: hsql: // localhost/hibernate
</Property>
<Property name = "connection. username"> SA </property>
<Property name = "connection. Password"> </property>
<Property name = "pool_size"> 5 </property>
<Property name = "show_ SQL"> false </property>
<Property name = "dialect">
Org. hibernate. dialect. hsqldialect
</Property>

<Mapping Resource = "motd. HBM. xml"/>

</Session-factory>
</Hibernate-configuration> [copy to clipboard]

In this example, show_ SQL is set to false, but can be set to true during debugging. You can easily see the SQL statements printed in the background. You also need to set the dialect attribute. It is best not to set it to genericdialect, an SQL subset acceptable to most databases, because it is not very convenient for expansion. Hibernate uses the dialect class to determine the optional SQL statements used to create and query databases.

Note: The file must be placed in classpath.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.