- Hibernate 教程
- Hibernate - 首頁
- ORM - 概述
- Hibernate - 概述
- Hibernate - 架構
- Hibernate - 環境
- Hibernate - 配置
- Hibernate - 會話
- Hibernate - 持久化類
- Hibernate - 對映檔案
- Hibernate - 對映型別
- Hibernate - 示例
- Hibernate - O/R 對映
- Hibernate - 級聯型別
- Hibernate - 註解
- Hibernate - 查詢語言
- Hibernate - Criteria 查詢
- Hibernate - 原生 SQL
- Hibernate - 快取
- Hibernate - 實體生命週期
- Hibernate - 批次處理
- Hibernate - 攔截器
- Hibernate - ID 生成器
- Hibernate - 儲存影像
- Hibernate - log4j 整合
- Hibernate - Spring 整合
- Hibernate - Struts 2 整合
- Hibernate - Web 應用程式
- 對映表示例
- Hibernate - 每個層次結構一張表
- Hibernate - 每個具體類一張表
- Hibernate - 每個子類一張表
- Hibernate 有用資源
- Hibernate - 問題解答
- Hibernate - 快速指南
- Hibernate - 有用資源
- Hibernate - 討論
Hibernate - 每個層次結構一張表
介紹
在 Hibernate 中,Java 類對映到資料庫表。假設我們有一個類,並且它有幾個子類,Hibernate 提供了三種將類對映到表的方式:
每個層次結構一張表
每個具體類一張表
每個子類一張表
讓我們詳細討論一下每個層次結構一張表,並舉例說明。
每個層次結構一張表
假設我們有一個類“Shape”,它有兩個子類,“Circle”和“Rectangle”。在這種型別的對映中,我們只會在資料庫中擁有一個表。
層次結構中有三個類。Shape 是超類,Circle 和 Rectangle 是子類。考慮type 變數。它的值對於 Shape 類是shape,對於 Circle 類是circle,對於 Rectangle 類是rectangle。這讓我們可以從資料庫中知道儲存的是哪種型別的物件。因此,它是鑑別器。
建立對映類
讓我們建立其資料需要持久化到資料庫中的 POJO 類。
Shape.java
package com.tutorialspoint;
public class Shape {
public int Id;
public String type;
public double area;
public Shape() {
this.type = "Shape";
}
public double calculateArea() {
return 0; // default implementation.
}
public int getId() {
return Id;
}
public void setId(int i) {
this.Id = i;
}
public String getType() {
return this.type;
}
public void setArea(double a) {
this.area = a;
}
public double getArea() {
return this.area;
}
}
Circle.java
package com.tutorialspoint;
import java.math.*;
public class Circle extends Shape {
private double radius;
public Circle(double rad ) {
this.radius = rad;
this.type = "Circle";
}
@Override
public double calculateArea() {
area = Math.PI * radius * radius;
return area;
}
}
Rectangle.java
package com.tutorialspoint;
public class Rectangle extends Shape {
private double length, width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
this.type = "Rectangle";
}
@Override
public double calculateArea() {
return length * width;
}
}
建立資料庫表
讓我們在資料庫中建立一個表。對於上述物件,將有一個表對應於您希望提供的永續性。請考慮需要將上述物件儲存和檢索到以下 RDBMS 表中:
CREATE TABLE students.shape( id int NOT NULL, type VARCHAR(20), radius int, length int, width int, area float );
建立對映配置檔案
現在建立一個對映檔案,該檔案指示 Hibernate 如何將定義的類對映到資料庫表。
shape.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.tutorialspoint.Shape" table="shape" discriminator-value="Shape">
<id name="id">
<generator class="increment"></generator>
</id>
<discriminator column="type" type="string"></discriminator>
<property name="area"></property>
<subclass name="com.tutorialspoint.Circle" discriminator-value="Circle">
<property name="radius"></property>
</subclass>
<subclass name="com.tutorialspoint.Rectangle" discriminator-value="Rectangle">
<property name="width"></property>
<property name="length"></property>
</subclass>
</class>
</hibernate-mapping>
建立 Hibernate 配置檔案
現在為資料庫和其他詳細資訊建立一個 hibernate 配置檔案。
hibernate.cfg.xml
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name = "hibernate.dialect">
org.hibernate.dialect.MySQL8Dialect
</property>
<property name = "hibernate.connection.driver_class">
com.mysql.cj.jdbc.Driver
</property>
<!—'students' is the database name -->
<property name = "hibernate.connection.url">
jdbc:mysql:///students
</property>
<property name = "hibernate.connection.username">
root
</property>
<property name = "hibernate.connection.password">
guest123
</property>
<!-- List of XML mapping files -->
<mapping resource = "shape.hbm.xml"/>
</session-factory>
</hibernate-configuration>
建立應用程式類
最後,我們將建立我們的應用程式類,其中包含 main() 方法來執行應用程式。我們將使用此應用程式來測試每個層次結構一張表的對映。
TestTablePerhierarchy.java
package com.tutorialspoint;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class TestTablePerHierarchy {
public static void main(String[] args) {
// create a hibernate configuration
StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();
// get the sessionfactory and open the session
SessionFactory factory=meta.getSessionFactoryBuilder().build();
Session session=factory.openSession();
// begin the transaction
Transaction t=session.beginTransaction();
// create Shape instance and set details to persist
Shape s1=new Shape();
s1.setType(s1.getType());
s1.setArea(s1.calculateArea());
// create Circle instance and set details to persist
Circle c1=new Circle(2);
c1.setType(c1.getType());
c1.setArea(c1.calculateArea());
// create Rectangle instance and set details to persist
Rectangle r1 = new Rectangle(3,4);
r1.setType(r1.getType());
r1.setArea(r1.calculateArea());
// persist all instances
session.persist(s1);
session.persist(c1);
session.persist(r1);
// commit the transaction and close the session
t.commit();
session.close();
System.out.println(" Successfully persisted 3 classes. Please check your database for results.");
}
}
編譯和執行
執行 TestTablePerHierarchy 二進位制檔案以執行程式。
輸出
您將獲得以下結果,並且記錄將建立在 Shape 表中。
$java TestTablePerHierarchy Successfully persisted 3 classes. Please check your database for results.
如果您檢查 Shape 表,它應該包含以下記錄:
mysql> select * from shape; +----+-----------+--------+--------+-------+-------+ | id | type | radius | length | width | area | +----+-----------+--------+--------+-------+-------+ | 1 | Shape | NULL | NULL | NULL | 0 | | 2 | Circle | 2 | NULL | NULL | 12.56 | | 3 | Rectangle | NULL | 3 | 4 | 12 | +----+-----------+--------+--------+-------+-------+ 3 rows in set (0.00 sec)
廣告