文章目錄
Hibernate配置文件用法
第一章:基本使用
第一節:實體類
Hibernate將數據庫表映射成爲Java的一個類,稱爲實體類。比如下面這個類:
public class User {
//類屬性對應數據庫字段,有些屬性可以不寫入數據庫
private int uid;
private String username;
private String password;
private String address;
//要被寫入數據庫的屬性必須有getter和setter方法
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [uid = " + uid + " username = " + username + " password = " + password + " address = " + address
+ " ]";
}
}
第二節:配置文件
每個實體類都要有配置文件,Hibernate自己也要有配置文件。注意,配置文件的頭部是固定的,不能變化的。
-
User的配置文件:對於上面User的配置文件如下,此配置文件由eclipse的Jboss-Hibernate插件自動產生:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Jun 5, 2020, 10:23:35 PM by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <!--name代表全類名,table代表數據庫中表的名字--> <class name="user.entity.User" table="t_user"> <!--id代表主鍵在實體類的屬性,column代表對應的數據庫中表的主鍵--> <id name="uid" type="int"> <column name="uid" unique="true"/> <generator class="native"/> </id> <!--property代表類的屬性--> <property generated="never" lazy="false" name="username" type="java.lang.String"> <column name="username"/> </property> <property generated="never" lazy="false" name="password" type="java.lang.String"> <column name="password"/> </property> <property generated="never" lazy="false" name="address" type="java.lang.String"> <column name="address"/> </property> </class> </hibernate-mapping>
-
Hibernate的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--第一部分: 配置數據庫信息,必須有,這裏使用的是postgre數據庫 --> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql:xxx</property> <property name="hibernate.connection.username">xxx</property> <property name="hibernate.connection.password">xxx</property> <!--第二部分: 配置hibernate信息, 輸出數據庫命令,可選 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 自動創建表和更新 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置方言 --> <property name="hibernate.dialec">org.hibernate.dialect.PostgreSQLDialect</property> <!-- 配置threadLocal --> <property name = "hibernate.current_session_context_class">thread</property> <!--第三部分: 把映射文件放到核心配置文件 --> <mapping resource="User.hbm.xml"/> </session-factory> </hibernate-configuration>
第二章:一對多表(多對一)關係映射
第一節:實體類
假設有客戶和聯繫人兩個實體類,每個客戶有多個聯繫人
-
客戶實體類
public class Customer { // 客戶id private Integer cid; // 客戶名字 private String custName; // 客戶等級 private String custLevel; // 客戶來源 private String custSource; // 客戶電話 private String custPhone; // 客戶手機 private String custMobile; // 主表要包含從表的集合或鏈表,來表示一對多關係 private Set<Contact> contacts = new HashSet<Contact>(); public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } public String getCustMobile() { return custMobile; } public void setCustMobile(String custMobile) { this.custMobile = custMobile; } public Set<Contact> getContacts() { return contacts; } public void setContacts(Set<Contact> contacts) { this.contacts = contacts; } public Set<Contact> getSetContact() { return contacts; } public void setContact(Set<Contact> contacts) { this.contacts = contacts; } }
-
聯繫人類
public class Contact { private Integer contact_id; // 聯繫人id主鍵 private String contact_name;// 聯繫人名字 private String contact_gender;// 聯繫人性別 private String contact_phone;// 聯繫人電話 // 從表要包含主表實體類,表示多對一關係 private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public Integer getContact_id() { return contact_id; } public void setContact_id(Integer contact_id) { this.contact_id = contact_id; } public String getContact_name() { return contact_name; } public void setContact_name(String contact_name) { this.contact_name = contact_name; } public String getContact_gender() { return contact_gender; } public void setContact_gender(String contact_gender) { this.contact_gender = contact_gender; } public String getContact_phone() { return contact_phone; } public void setContact_phone(String contact_phone) { this.contact_phone = contact_phone; } }
第二節:配置文件
-
主表配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 1 配置類和表對應 class標籤 name屬性:實體類全路徑 table屬性:數據庫表名稱 --> <class name="entity.Customer" table="t_customer"> <id name="cid" column="cid"> <generator class="native"></generator> </id> <property name="custName" column="custName"></property> <property name="custLevel" column="custLevel"></property> <property name="custSource" column="custSource"></property> <property name="custPhone" column="custPhone"></property> <property name="custMobile" column="custMobile"></property> <!-- 在客戶映射文件中,表示所有聯繫人 使用set標籤表示所有聯繫人 set標籤裏面有name屬性: 屬性值寫在客戶實體類裏面表示聯繫人的set集合名稱 inverse屬性默認值:false不放棄關係維護 true表示放棄關係維護 cascade-級聯保存:save-update -級聯刪除:delete --> <set name="contacts" inverse="true" cascade="save-update,delete"> <!-- 一對多建表,有外鍵 hibernate機制:雙向維護外鍵,在一和多那一方都配置外鍵 column屬性值,對應數據庫表中的列:外鍵名稱 --> <key column="ccid"></key> <!-- 客戶所有的聯繫人,class裏面寫聯繫人實體類全路徑 --> <one-to-many class="entity.Contact"/> </set> </class> </hibernate-mapping>
-
從表配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 1 配置類和表對應 class標籤 name屬性:實體類全路徑 table屬性:數據庫表名稱 --> <class name="entity.Contact" table="t_contact"> <id name="contact_id" column="contact_id"> <generator class="native"></generator> </id> <property name="contact_name" column="contact_name"></property> <property name="contact_gender" column="contact_gender"></property> <property name="contact_phone" column="contact_phone"></property> <!-- 表示聯繫人所屬客戶 name屬性:因爲在聯繫人實體類使用customer對象表示,寫customer名稱 class屬性:customer全路徑 column屬性, 對應數據庫表中的列:外鍵名稱 --> <many-to-one name="customer" class="entity.Customer" column="ccid"></many-to-one> </class> </hibernate-mapping>
-
Hibernate配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--第一部分: 配置數據庫信息,必須有,這裏使用的是postgre數據庫 --> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql:xxx</property> <property name="hibernate.connection.username">xxx</property> <property name="hibernate.connection.password">xxx</property> <!--第二部分: 配置hibernate信息,可選 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 自動創建表和更新 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置方言 --> <property name="hibernate.dialec">org.hibernate.dialect.PostgreSQLDialect</property> <!-- 配置threadLocal --> <property name = "hibernate.current_session_context_class">thread</property> <!--第三部分: 把映射文件放到核心配置文件 --> <mapping resource="Customer.hbm.xml"/> <mapping resource="Contact.hbm.xml"/> </session-factory> </hibernate-configuration>
第三章:多對多關係映射
第一節:實體類
假設兩個實體類,User和Role爲多對多關係
-
User實體類
public class User { private Integer user_id; private String user_name; private String user_password; // 多對多關係映射互相包含集合 private Set<Role> rloeSet = new HashSet<Role>(); public Set<Role> getRloeSet() { return rloeSet; } public void setRloeSet(Set<Role> rloeSet) { this.rloeSet = rloeSet; } public Integer getUser_id() { return user_id; } public void setUser_id(Integer user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } }
-
Role實體類
public class Role { private Integer role_id; private String role_name; private String role_memo; // 多對多關係映射互相包含集合 private Set<User> userSet = new HashSet<User>(); public Set<User> getUserSet() { return userSet; } public void setUserSet(Set<User> userSet) { this.userSet = userSet; } public Integer getRole_id() { return role_id; } public void setRole_id(Integer role_id) { this.role_id = role_id; } public String getRole_name() { return role_name; } public void setRole_name(String role_name) { this.role_name = role_name; } public String getRole_memo() { return role_memo; } public void setRole_memo(String role_memo) { this.role_memo = role_memo; } }
第二節:配置文件
多對多關係需要第三張表來維護關係,所以會生成三張表
-
User配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 1 配置類和表對應 class標籤 name屬性:實體類全路徑 table屬性:數據庫表名稱 --> <class name="entity.User" table="t_user"> <id name="user_id" column="user_id"> <generator class="native"></generator> </id> <property name="user_name" column="user_name"></property> <property name="user_password" column="user_password"></property> <set name="rloeSet" table="user_role" cascade="save-update,delete"> <!-- 當前文件在第三張表的外鍵名稱 --> <key column="user_id"></key> <!-- class代表set中類的全類名,cloumn代表其在數據庫表中的字段 --> <many-to-many class="entity.Role" column="role_id"/> </set> </class> </hibernate-mapping>
-
Role配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 1 配置類和表對應 class標籤 name屬性:實體類全路徑 table屬性:數據庫表名稱 --> <class name="entity.Role" table="t_role"> <id name="role_id" column="role_id"> <generator class="native"></generator> </id> <property name="role_name" column="role_name"></property> <property name="role_memo" column="role_memo"></property> <set name="userSet" table="user_role"> <!-- 當前文件在第三張表的外鍵名稱 --> <key column="role_id"></key> <!-- class代表set中類的全類名,cloumn代表其在數據庫表中的字段 --> <many-to-many class="entity.User" column="user_id"/> </set> </class> </hibernate-mapping>