Skip to content

Commit 085ef15

Browse files
committed
model: add equals, hashcode, toString methods to entities
1 parent 3d2c810 commit 085ef15

File tree

3 files changed

+141
-71
lines changed

3 files changed

+141
-71
lines changed
Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
1-
name: CI Pipeline with Maven
2-
3-
on:
4-
push:
5-
branches:
6-
- dev
7-
pull_request:
8-
branches:
9-
- dev
10-
- master
11-
12-
permissions:
13-
contents: write
14-
pull-requests: write
15-
16-
jobs:
17-
build:
18-
runs-on: ubuntu-latest
19-
20-
services:
21-
postgres:
22-
image: postgres:17-alpine
23-
env:
24-
POSTGRES_DB: subscription_service_app
25-
POSTGRES_USER: postgres
26-
POSTGRES_PASSWORD: root
27-
ports:
28-
- 5433:5432
29-
options: >-
30-
--health-cmd "pg_isready -U postgres -d subscription_service_app"
31-
--health-interval 10s
32-
--health-timeout 5s
33-
--health-retries 5
34-
35-
steps:
36-
- name: Checkout code
37-
uses: actions/checkout@v4
38-
39-
- name: Set up JDK 17
40-
uses: actions/setup-java@v3
41-
with:
42-
distribution: 'temurin'
43-
java-version: '17'
44-
cache: 'maven'
45-
46-
- name: Grant execute permission for mvnw
47-
run: chmod +x mvnw
48-
49-
- name: Build with Maven
50-
run: ./mvnw clean -B package --file pom.xml -Dspring.profiles.active=dev
51-
52-
- name: Send Telegram notification
53-
if: always()
54-
env:
55-
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
56-
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
57-
run: |
58-
STATUS="${{ job.status }}"
59-
60-
MESSAGE="🚀 CI Pipeline Status: ${STATUS}
61-
62-
Project: \#$(basename ${{ github.repository }} | sed 's/-/\\-/g')
63-
Branch: \`${{ github.ref }}\`
64-
Commit: \`${{ github.sha }}\`
65-
66-
[Details](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
67-
68-
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
69-
-d "chat_id=${TELEGRAM_CHAT_ID}" \
70-
-d "text=${MESSAGE}" \
1+
name: CI Pipeline with Maven
2+
3+
on:
4+
push:
5+
branches:
6+
- dev
7+
pull_request:
8+
branches:
9+
- dev
10+
- master
11+
12+
permissions:
13+
contents: write
14+
pull-requests: write
15+
16+
jobs:
17+
build:
18+
runs-on: ubuntu-latest
19+
20+
services:
21+
postgres:
22+
image: postgres:17-alpine
23+
env:
24+
POSTGRES_DB: subscription_service_app
25+
POSTGRES_USER: postgres
26+
POSTGRES_PASSWORD: root
27+
ports:
28+
- 5433:5432
29+
options: >-
30+
--health-cmd "pg_isready -U postgres -d subscription_service_app"
31+
--health-interval 10s
32+
--health-timeout 5s
33+
--health-retries 5
34+
35+
steps:
36+
- name: Checkout code
37+
uses: actions/checkout@v4
38+
39+
- name: Set up JDK 17
40+
uses: actions/setup-java@v3
41+
with:
42+
distribution: 'temurin'
43+
java-version: '17'
44+
cache: 'maven'
45+
46+
- name: Grant execute permission for mvnw
47+
run: chmod +x mvnw
48+
49+
- name: Build with Maven
50+
run: ./mvnw clean -B package --file pom.xml -Dspring.profiles.active=dev
51+
52+
- name: Send Telegram notification
53+
if: always()
54+
env:
55+
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
56+
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
57+
run: |
58+
STATUS="${{ job.status }}"
59+
60+
MESSAGE="🚀 CI Pipeline Status: ${STATUS}
61+
62+
Project: \#$(basename ${{ github.repository }} | sed 's/-/\\-/g')
63+
Branch: \`${{ github.ref }}\`
64+
Commit: \`${{ github.sha }}\`
65+
66+
[Details](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
67+
68+
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
69+
-d "chat_id=${TELEGRAM_CHAT_ID}" \
70+
-d "text=${MESSAGE}" \
7171
-d "parse_mode=MarkdownV2"

src/main/java/com/github/tennyros/subscription_service/model/Subscription.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
import lombok.Getter;
1515
import lombok.NoArgsConstructor;
1616
import lombok.Setter;
17+
import lombok.ToString;
18+
import org.hibernate.proxy.HibernateProxy;
1719

1820
import java.time.LocalDate;
21+
import java.util.Objects;
1922

2023
@Entity
2124
@Getter
2225
@Setter
2326
@Builder
27+
@ToString
2428
@NoArgsConstructor
2529
@AllArgsConstructor
2630
@Table(name = "subscriptions")
@@ -37,8 +41,39 @@ public class Subscription {
3741
@Column(name = "start_date", nullable = false)
3842
private LocalDate startDate;
3943

40-
@ManyToOne(fetch = FetchType.LAZY)
44+
@ToString.Exclude
4145
@JoinColumn(name = "user_id")
46+
@ManyToOne(fetch = FetchType.LAZY)
4247
private User user;
4348

49+
@Override
50+
public final boolean equals(Object o) {
51+
if (this == o) return true;
52+
if (o == null) return false;
53+
54+
if (!(o instanceof Subscription subscription)) {
55+
return false;
56+
}
57+
58+
Class<?> oEffectiveClass = (o instanceof HibernateProxy hibernateProxy)
59+
? hibernateProxy.getHibernateLazyInitializer()
60+
.getPersistentClass()
61+
: o.getClass();
62+
Class<?> thisEffectiveClass = (o instanceof HibernateProxy hibernateProxy)
63+
? hibernateProxy.getHibernateLazyInitializer()
64+
.getPersistentClass()
65+
: this.getClass();
66+
if (thisEffectiveClass != oEffectiveClass) return false;
67+
return id != null && Objects.equals(id, subscription.id);
68+
}
69+
70+
@Override
71+
public final int hashCode() {
72+
return (this instanceof HibernateProxy thisProxy)
73+
? thisProxy.getHibernateLazyInitializer()
74+
.getPersistentClass()
75+
.hashCode()
76+
: getClass().hashCode();
77+
}
78+
4479
}

src/main/java/com/github/tennyros/subscription_service/model/User.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
import lombok.Getter;
1414
import lombok.NoArgsConstructor;
1515
import lombok.Setter;
16+
import lombok.ToString;
17+
import org.hibernate.proxy.HibernateProxy;
1618

1719
import java.util.ArrayList;
1820
import java.util.List;
21+
import java.util.Objects;
1922

2023
import static jakarta.persistence.CascadeType.ALL;
2124

2225
@Entity
2326
@Setter
2427
@Getter
2528
@Builder
29+
@ToString
2630
@NoArgsConstructor
2731
@AllArgsConstructor
2832
@Table(name = "users")
@@ -37,7 +41,38 @@ public class User {
3741
private String email;
3842

3943
@Builder.Default
44+
@ToString.Exclude
4045
@OneToMany(mappedBy = "user", cascade = { ALL }, orphanRemoval = true, fetch = FetchType.LAZY)
4146
private List<Subscription> subscriptions = new ArrayList<>();
4247

48+
@Override
49+
public final boolean equals(Object o) {
50+
if (this == o) return true;
51+
if (o == null) return false;
52+
53+
if (!(o instanceof User user)) {
54+
return false;
55+
}
56+
57+
Class<?> oEffectiveClass = (o instanceof HibernateProxy hibernateProxy)
58+
? hibernateProxy.getHibernateLazyInitializer()
59+
.getPersistentClass()
60+
: o.getClass();
61+
Class<?> thisEffectiveClass = (o instanceof HibernateProxy hibernateProxy)
62+
? hibernateProxy.getHibernateLazyInitializer()
63+
.getPersistentClass()
64+
: this.getClass();
65+
if (thisEffectiveClass != oEffectiveClass) return false;
66+
return id != null && Objects.equals(id, user.id);
67+
}
68+
69+
@Override
70+
public final int hashCode() {
71+
return (this instanceof HibernateProxy thisProxy)
72+
? thisProxy.getHibernateLazyInitializer()
73+
.getPersistentClass()
74+
.hashCode()
75+
: getClass().hashCode();
76+
}
77+
4378
}

0 commit comments

Comments
 (0)