Hibernate 常用功能记录

Hibernate 常用功能记录

简介

Hibernate 是一个开源的对象关系映射框架,它对 JDBC 的操作数据库的过程进行封装,使得开发者只需要关注业务逻辑,而不需要关注底层的数据库操作。Hibernate 有着良好的性能,支持多种数据库,同时也支持多种编程语言。

常用功能

关联(Associations)

@ManyToOne

用户类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity(name = "Person")
public class Person {

@Id
@GeneratedValue
private Long id;

}

联系方式类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity(name = "Phone")
public class Phone {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String number;

@ManyToOne(optional = false)
@JoinColumn(name = "person_id", nullable = false, foreignKey = @ForeignKey(name = "PERSON_ID_FK"))
private Person person;

}
@OneToMany

用户类

1
2
3
4
5
6
7
8
9
10
11
@Entity(name = "Person")
public class Person {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

@OneToMany(mappedBy = "person",cascade = CascadeType.ALL, orphanRemoval = true)
private List<Phone> phones = new ArrayList<>();

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Entity(name = "Phone")
public static class Phone {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

private String number;

@ManyToOne
private Person person;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Phone phone = (Phone) o;
return Objects.equals(number, phone.number);
}

@Override
public int hashCode() {
return Objects.hash(number);
}

}
@OneToOne

联系方式表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Entity(name = "Phone")
public class Phone {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

@Column(name = "number")
private String number;

@OneToOne
@JoinColumn(name = "details_id")
private PhoneDetails details;

}

联系方式详情表:

1
2
3
4
5
6
7
8
9
10
11
12
@Entity(name = "PhoneDetails")
public class PhoneDetails {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

private String provider;

private String technology;

}
@ManyToMany

用户表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Entity(name = "Person")
public class Person {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

@NaturalId
private String registrationNumber;

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<Address> addresses = new ArrayList<>();

public void addAddress(Address address) {
addresses.add(address);
address.getOwners().add(this);
}

public void removeAddress(Address address) {
addresses.remove(address);
address.getOwners().remove(this);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Person person = (Person) o;
return Objects.equals(registrationNumber, person.registrationNumber);
}

@Override
public int hashCode() {
return Objects.hash(registrationNumber);
}

}

地址表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@Entity(name = "Address")
public class Address {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

private String street;

private String number;

private String postalCode;

@ManyToMany(mappedBy = "addresses")
private List<Person> owners = new ArrayList<>();

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Address address = (Address) o;
return Objects.equals(street, address.street) &&
Objects.equals(number, address.number) &&
Objects.equals(postalCode, address.postalCode);
}

@Override
public int hashCode() {
return Objects.hash(street, number, postalCode);
}

}
@NotFound

联系方式类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Entity(name = "Phone")
public class Phone {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

@Column(name = "number")
private String number;

@OneToOne
@NotFound(action = NotFoundAction.EXCEPTION)
@JoinColumn(name = "details_id")
private PhoneDetails details;

}

联系方式详情类:

1
2
3
4
5
6
7
8
9
10
11
12
@Entity(name = "PhoneDetails")
public class PhoneDetails {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;

private String provider;

private String technology;

}
@Any

注:略,不清楚有什么用。

@JoinFormula

注:略,不清楚有什么用。

@JoinColumnOrFormula

注:略,不清楚有什么用。

参考资料

官方文档


Hibernate 常用功能记录
https://wangqian0306.github.io/2023/hibernate/
作者
WangQian
发布于
2023年4月20日
许可协议