4.2 配置Hibernate
点击此处获得本文中的实例代码,这样你可以对本例有更深的了解。
为了运行实例,请确信你已经下载过Hibernate和log4j的最新发布包,同时也要把数据库驱动放到classpath中。下载以后的压缩包中有example_schema.ddl文件用来生成数据库表。
接下来作者给我们讲述了一个Hibernate.properties文件,它是在配置Hibernate的时候最先接触到的,它在应用程序启动的时候为 我们进行初始化工作(译者注:有了hibernate.cfg.xml,Hibernate.properties变的可有可无了,不是吗?)
hibernate.connection.driver_class=COM.ibm.db2.jdbc.net.DB2Driver
hibernate.connection.url=jdbc:db2://server1/sample
hibernate.connection.username=db2admin
hibernate.connection.password=password
hibernate.default_schema=db2admin
hibernate.dialect=net.sf.hibernate.dialect.DB2Dialect
hibernate.show_sql=true
# The maximum number of active connections that can be allocated # from this pool at the same time, or zero for no limit.
hibernate.dbcp.maxActive 100
# Action to take in case of an exhausted DBCP statement pool
# ( 0 = fail, 1 = block, 2= grow)
hibernate.dbcp.whenExhaustedAction 1
hibernate.dbcp.maxWait 120000
# The maximum number of active connections that can remain
# idle in the pool, without extra ones being released, or zero
# for no limit.
hibernate.dbcp.maxIdle 10
# The SQL query that will be used to validate
# connections from this pool before returning them to the caller.
# hibernate.dbcp.validationQuery=TODO
## prepared statement cache
hibernate.dbcp.ps.maxActive 100
# Action to take in case of an exhausted DBCP statement
#pool ( 0 = fail, 1 = block, 2= grow)
hibernate.dbcp.ps.whenExhaustedAction 1
# The maximum number of milliseconds that the pool will
# wait (when there are no available connections) for a connection
# to be returned before throwing an exception, or -1 to
# wait indefinitely.
hibernate.dbcp.ps.maxWait 120000
hibernate.dbcp.ps.maxIdle 100
上边的代码中,首先指明了和数据连接有关的属性元素:database driver、JDBC URL、用户账号和密码、dialect("数据库"方言、土语、地方话)等等,dialect为我们使用的每一个数据库进行最佳优化,在 Hibernate使用手册中你可以到得到每一个数据库的dialect.最后,hibernate.show_sql当设定为"真"的时候,我们可以在 Hibernate的DEBUG信息中看到HQL在执行的时候的SQL语句。
剩下的属性元素是用来配置连接池的,这里使用的是用Jakarta DBCP(详细信息到Jakarta官方网站查看)来实现连接池,同样Hibernate也可以用其它的方式来实现此功能,如:C3PO(没听说过,呵呵。。)。详细信息进入Hibernate文档。
4.3 创建持久对象
在Hibernate运行环境搭起来以后,我们开始创建持久对象或是映射文件来开始我们的工作。(通常创建对象和创建映射文件做其一即可,另一个可以通过 做好的来自动完成),这里我们从创建持久对象开始,下面是完成以后的代码,Hibernate所需要的"持久对象"符合我们经常写的对象的规范,它们没什 么差别:
package dbdemo;
import java.util.Date;
import java.util.Set;
/**
* @hibernate.class table="Users"
*
* @author MEagle
*
* Represents a User
*/
public class User {
private String userID;
private String userName;
private String password;
private String emailAddress;
private Date lastLogon;
private Set contacts;
private Set books;
private Address address;
/**
* @hibernate.property column="EmailAddress" type="string"
* @return String
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* @hibernate.property column="LastLogon" type="date"
* @return Date
*/
public Date getLastLogon() {
return lastLogon;
}
/**
* @hibernate.property column="Password" type="string"
* @return String
*/
public String getPassword() {
return password;
}
/**
* @hibernate.id generator-class="assigned" type="string"
* column="LogonID"
* @return String
*/
public String getUserID() {
return userID;
}
/**
* @hibernate.property column="Name" type="string"
* @return String
*/
public String getUserName() {
return userName;
}
/**
* @param string
*/
public void setEmailAddress(String string) {
emailAddress = string;
}
/**
* @param string
*/
public void setLastLogon(Date date) {
lastLogon = date;
}
/**
* @param string
*/
public void setPassword(String string) {
password = string;
}
/**
* @param string
*/
public void setUserID(String string) {
userID = string;
}
/**
* @param string
*/
public void setUserName(String string) {
userName = string;
}
/**
* @hibernate.set role="contacts" table="Contacts"
* cascade="all" readonly="true"
* @hibernate.collection-key column="User_ID"
* @hibernate.collection-one-to-many class="dbdemo.Contact"
* @return java.util.Set
*/
public Set getContacts() {
return contacts;
}
/**
* @param set
*/
public void setContacts(Set set) {
contacts = set;
}
/**
* @hibernate.set role="books" table="Book_User_Link"
* cascade="all" eadonly="true"
* @hibernate.collection-key column="UserID"
* @hibernate.collection-many-to-many
* class="dbdemo.Book" column="BookID"
* @return java.util.Set
*/
public Set getBooks() {
return books;
}
/**
* @param set
*/
public void setBooks(Set set) {
books = set;
}
/**
* @hibernate.one-to-one class="dbdemo.Address"
* @return dbdemo.Address
*/
public Address getAddress() {
return address;
}
/**
* @param address
*/
public void setAddress(Address address) {
this.address = address;
}
}
点击此处获得本文中的实例代码,这样你可以对本例有更深的了解。
为了运行实例,请确信你已经下载过Hibernate和log4j的最新发布包,同时也要把数据库驱动放到classpath中。下载以后的压缩包中有example_schema.ddl文件用来生成数据库表。
接下来作者给我们讲述了一个Hibernate.properties文件,它是在配置Hibernate的时候最先接触到的,它在应用程序启动的时候为 我们进行初始化工作(译者注:有了hibernate.cfg.xml,Hibernate.properties变的可有可无了,不是吗?)
hibernate.connection.driver_class=COM.ibm.db2.jdbc.net.DB2Driver
hibernate.connection.url=jdbc:db2://server1/sample
hibernate.connection.username=db2admin
hibernate.connection.password=password
hibernate.default_schema=db2admin
hibernate.dialect=net.sf.hibernate.dialect.DB2Dialect
hibernate.show_sql=true
# The maximum number of active connections that can be allocated # from this pool at the same time, or zero for no limit.
hibernate.dbcp.maxActive 100
# Action to take in case of an exhausted DBCP statement pool
# ( 0 = fail, 1 = block, 2= grow)
hibernate.dbcp.whenExhaustedAction 1
hibernate.dbcp.maxWait 120000
# The maximum number of active connections that can remain
# idle in the pool, without extra ones being released, or zero
# for no limit.
hibernate.dbcp.maxIdle 10
# The SQL query that will be used to validate
# connections from this pool before returning them to the caller.
# hibernate.dbcp.validationQuery=TODO
## prepared statement cache
hibernate.dbcp.ps.maxActive 100
# Action to take in case of an exhausted DBCP statement
#pool ( 0 = fail, 1 = block, 2= grow)
hibernate.dbcp.ps.whenExhaustedAction 1
# The maximum number of milliseconds that the pool will
# wait (when there are no available connections) for a connection
# to be returned before throwing an exception, or -1 to
# wait indefinitely.
hibernate.dbcp.ps.maxWait 120000
hibernate.dbcp.ps.maxIdle 100
上边的代码中,首先指明了和数据连接有关的属性元素:database driver、JDBC URL、用户账号和密码、dialect("数据库"方言、土语、地方话)等等,dialect为我们使用的每一个数据库进行最佳优化,在 Hibernate使用手册中你可以到得到每一个数据库的dialect.最后,hibernate.show_sql当设定为"真"的时候,我们可以在 Hibernate的DEBUG信息中看到HQL在执行的时候的SQL语句。
剩下的属性元素是用来配置连接池的,这里使用的是用Jakarta DBCP(详细信息到Jakarta官方网站查看)来实现连接池,同样Hibernate也可以用其它的方式来实现此功能,如:C3PO(没听说过,呵呵。。)。详细信息进入Hibernate文档。
4.3 创建持久对象
在Hibernate运行环境搭起来以后,我们开始创建持久对象或是映射文件来开始我们的工作。(通常创建对象和创建映射文件做其一即可,另一个可以通过 做好的来自动完成),这里我们从创建持久对象开始,下面是完成以后的代码,Hibernate所需要的"持久对象"符合我们经常写的对象的规范,它们没什 么差别:
package dbdemo;
import java.util.Date;
import java.util.Set;
/**
* @hibernate.class table="Users"
*
* @author MEagle
*
* Represents a User
*/
public class User {
private String userID;
private String userName;
private String password;
private String emailAddress;
private Date lastLogon;
private Set contacts;
private Set books;
private Address address;
/**
* @hibernate.property column="EmailAddress" type="string"
* @return String
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* @hibernate.property column="LastLogon" type="date"
* @return Date
*/
public Date getLastLogon() {
return lastLogon;
}
/**
* @hibernate.property column="Password" type="string"
* @return String
*/
public String getPassword() {
return password;
}
/**
* @hibernate.id generator-class="assigned" type="string"
* column="LogonID"
* @return String
*/
public String getUserID() {
return userID;
}
/**
* @hibernate.property column="Name" type="string"
* @return String
*/
public String getUserName() {
return userName;
}
/**
* @param string
*/
public void setEmailAddress(String string) {
emailAddress = string;
}
/**
* @param string
*/
public void setLastLogon(Date date) {
lastLogon = date;
}
/**
* @param string
*/
public void setPassword(String string) {
password = string;
}
/**
* @param string
*/
public void setUserID(String string) {
userID = string;
}
/**
* @param string
*/
public void setUserName(String string) {
userName = string;
}
/**
* @hibernate.set role="contacts" table="Contacts"
* cascade="all" readonly="true"
* @hibernate.collection-key column="User_ID"
* @hibernate.collection-one-to-many class="dbdemo.Contact"
* @return java.util.Set
*/
public Set getContacts() {
return contacts;
}
/**
* @param set
*/
public void setContacts(Set set) {
contacts = set;
}
/**
* @hibernate.set role="books" table="Book_User_Link"
* cascade="all" eadonly="true"
* @hibernate.collection-key column="UserID"
* @hibernate.collection-many-to-many
* class="dbdemo.Book" column="BookID"
* @return java.util.Set
*/
public Set getBooks() {
return books;
}
/**
* @param set
*/
public void setBooks(Set set) {
books = set;
}
/**
* @hibernate.one-to-one class="dbdemo.Address"
* @return dbdemo.Address
*/
public Address getAddress() {
return address;
}
/**
* @param address
*/
public void setAddress(Address address) {
this.address = address;
}
}