文章目录
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>