1.概述
關(guān)系數(shù)據(jù)庫的表之間不存在繼承關(guān)系资锰,為了將面向?qū)ο笾械睦^承關(guān)系映射到關(guān)系數(shù)據(jù)庫中,可使用三種映射策略:
1追他、每個(gè)繼承層次一張表
2便瑟、每個(gè)具體類一張表
3仪壮、每個(gè)類一張表
2.每個(gè)繼承層次一張表
即用一張表表示一個(gè)繼承層次,因此需要增加一個(gè)字段用于區(qū)別
public class Person {
private int id;
private String name;
private String sex;
private int age;
}
public class Student extends Person {
private String sno;
private String school;
}
public class Worker extends Person{
private String no;
private double salary;
}
映射語法:
1胳徽、定義一個(gè)以父類命名的映射文件
2、<id>標(biāo)記對(duì)后面添加<discriminator>標(biāo)記爽彤,并指定column屬性來定義鑒別字段
3养盗、父類屬性放在<class>標(biāo)記對(duì)之間,子類屬性放在<subclass>標(biāo)記對(duì)之間
4适篙、在各類標(biāo)記中使用discriminator-value屬性指定各類對(duì)應(yīng)的鑒別字段的值
示例:
1往核、數(shù)據(jù)庫連接
2、為項(xiàng)目添加Hibernate支持
3嚷节、創(chuàng)建持久化類
4聂儒、創(chuàng)建映射文件
5虎锚、修改hibernate配置文件
6、創(chuàng)建工具類獲取Session實(shí)例
7衩婚、編寫Dao接口及其實(shí)現(xiàn)類
8窜护、測(cè)試
項(xiàng)目結(jié)構(gòu):
Person類:
public class Person {
private int id;
private String name;
private String sex;
private int age;
public Person() {}
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Worker類:
public class Worker extends Person{
private String no;
private double salary;
public Worker() {}
public Worker(String name, String sex, int age, String no, double salary) {
super(name, sex, age);
this.no = no;
this.salary = salary;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
Student類:
public class Student extends Person {
private String sno;
private String school;
public Student() {
}
public Student(String name, String sex, int age, String sno, String school) {
super(name, sex, age);
this.sno = sno;
this.school = school;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
工具類:
public class HibernateUtil {
private static SessionFactory sf;
// 工具類不允許實(shí)例化
private HibernateUtil() {
}
// 靜態(tài)代碼塊,只執(zhí)行一次非春,提高性能
static {
Configuration cf = new Configuration().configure();
sf = cf.buildSessionFactory();
}
public static Session getSession() {
Session session = sf.getCurrentSession();
return session;
}
}
Dao及其實(shí)現(xiàn)類:
public interface HibernateDao {
public void addPerson(Person person);
@SuppressWarnings("unchecked")
public List getAllPerson(String hql);
}
public class HibernateDaoImpl implements HibernateDao {
@Override
public void addPerson(Person person) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
session.save(person);
tx.commit();
} catch (Exception e) {
if (null != tx) {
tx.rollback();
}
e.printStackTrace();
}
}
@Override
@SuppressWarnings("unchecked")
public List getAllPerson(String hql) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(hql);
List persons = query.list();
tx.commit();
return persons;
}
}
測(cè)試類:
public class HibernateTest {
public static void main(String[] args) {
Person person = new Person("張三", "男", 20);
Worker worker = new Worker("李四", "女", 28, "P0001", 6000);
Student student = new Student("王五", "女", 19, "SD001", "廣金");
HibernateDao hibernateDao = new HibernateDaoImpl();
hibernateDao.addPerson(person);
hibernateDao.addPerson(student);
hibernateDao.addPerson(worker);
String hql = "from Person";
List<Person> persons = hibernateDao.getAllPerson(hql);
System.out.println("========所有人員信息=======");
for (Person p : persons) {
System.out.println("姓名:" + p.getName() + " 性別:" + p.getSex() + " 年齡:" + p.getAge());
}
hql = "from Worker";
List<Worker> workers = hibernateDao.getAllPerson(hql);
System.out.println("========工人信息=======");
for (Worker w : workers) {
System.out.println("姓名:" + w.getName() + " 性別:" + w.getSex() + " 年齡:" + w.getAge() +
" 工號(hào):" + w.getNo() + "薪水:" + w.getSalary());
}
hql = "from Student";
List<Student> students = hibernateDao.getAllPerson(hql);
System.out.println("========學(xué)生信息=======");
for (Student s : students) {
System.out.println("姓名:" + s.getName() + " 性別:" + s.getSex() + " 年齡:" + s.getAge() +
" 學(xué)號(hào):" + s.getSno() + "學(xué)校:" + s.getSchool());
}
}
}
Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="這里寫你的Person類所在的包">
<!--ORM對(duì)象柱徙、關(guān)系數(shù)據(jù)庫映射-->
<class name="Person" table="person" discriminator-value="person">
<id name="id" type="integer">
<generator class="native"/>
</id>
<!--設(shè)置鑒別字段-->
<discriminator column="type" type="string" length="15"/>
<!--從父類繼承下來的共享屬性-->
<property name="name" type="string" column="username" length="12" not-null="true"/>
<property name="sex" type="string" length="1"/>
<property name="age" type="integer"/>
<subclass name="Student" discriminator-value="student">
<!--子類新增屬性-->
<property name="sno" type="string" length="10"/>
<property name="school" type="string" length="50"/>
</subclass>
<subclass name="Worker" discriminator-value="worker">
<!--子類新增屬性-->
<property name="no" column="wno" type="string" length="10"/>
<property name="salary" type="double"/>
</subclass>
</class>
</hibernate-mapping>
hibernate配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useSSL=true</property>
<property name="connection.username">root</property>
<property name="connection.password">這里填你的密碼</property>
<!--連接池設(shè)置-->
<property name="connection.pool_size">2</property>
<!--數(shù)據(jù)庫方言設(shè)置-->
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!--向控制臺(tái)顯示執(zhí)行的SQL語句-->
<property name="show_sql">false</property>
<!--格式化SQL語句后輸出-->
<property name="hibernate.format_sql">true</property>
<!--事務(wù)配置-->
<property name="current_session_context_class">thread</property>
<!--創(chuàng)建SessionFactory對(duì)象時(shí)自動(dòng)創(chuàng)建數(shù)據(jù)庫表,可取create、create-drop奇昙、update等值-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--配置映射文件-->
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
運(yùn)行結(jié)果:
數(shù)據(jù)庫中:
補(bǔ)充說明:需要先建立數(shù)據(jù)庫hibernate护侮,此項(xiàng)目為maven項(xiàng)目。也可以建立普通java項(xiàng)目或web項(xiàng)目储耐。
一個(gè)繼承層次一張表的優(yōu)缺點(diǎn)如下:
1羊初、最簡(jiǎn)單,執(zhí)行效率最高(無需關(guān)聯(lián))
2什湘、存在冗余字段长赞,需要加入?yún)^(qū)分各類的字段;不允許子類屬性為not null約束(允許為空)禽炬。