Skip to content

Commit 8535479

Browse files
Setting up AbstractEntity
1 parent 56e8db1 commit 8535479

4 files changed

Lines changed: 37 additions & 53 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.launchcode.codingevents.models;
2+
3+
import jakarta.persistence.GeneratedValue;
4+
import jakarta.persistence.Id;
5+
import jakarta.persistence.MappedSuperclass;
6+
7+
import java.util.Objects;
8+
9+
@MappedSuperclass
10+
public abstract class AbstractEntity {
11+
12+
@Id
13+
@GeneratedValue
14+
private int id;
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
@Override
21+
public boolean equals(Object o) {
22+
if (this == o) return true;
23+
if (o == null || getClass() != o.getClass()) return false;
24+
AbstractEntity entity = (AbstractEntity) o;
25+
return id == entity.id;
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
return Objects.hash(id);
31+
}
32+
33+
}

src/main/java/org/launchcode/codingevents/models/Event.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
package org.launchcode.codingevents.models;
22

33
import jakarta.persistence.Entity;
4-
import jakarta.persistence.GeneratedValue;
5-
import jakarta.persistence.Id;
64
import jakarta.validation.constraints.Email;
75
import jakarta.validation.constraints.NotBlank;
86
import jakarta.validation.constraints.Size;
97

10-
import java.util.Objects;
11-
128
/**
139
* Created by Chris Bay
1410
*/
1511
@Entity
16-
public class Event {
17-
18-
@Id
19-
@GeneratedValue
20-
private int id;
12+
public class Event extends AbstractEntity {
2113

2214
@NotBlank(message = "Name is required")
2315
@Size(min = 3, max = 50, message = "Name must be between 3 and 50 characters")
@@ -73,25 +65,9 @@ public void setType(EventType type) {
7365
this.type = type;
7466
}
7567

76-
public int getId() {
77-
return id;
78-
}
79-
8068
@Override
8169
public String toString() {
8270
return name;
8371
}
8472

85-
@Override
86-
public boolean equals(Object o) {
87-
if (this == o) return true;
88-
if (o == null || getClass() != o.getClass()) return false;
89-
Event event = (Event) o;
90-
return id == event.id;
91-
}
92-
93-
@Override
94-
public int hashCode() {
95-
return Objects.hash(id);
96-
}
9773
}
Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
package org.launchcode.codingevents.models;
22

33
import jakarta.persistence.Entity;
4-
import jakarta.persistence.GeneratedValue;
5-
import jakarta.persistence.Id;
64
import jakarta.validation.constraints.Size;
75

8-
import java.util.Objects;
9-
106
/**
117
* Created by Chris Bay
128
*/
139
@Entity
14-
public class EventCategory {
15-
16-
@Id
17-
@GeneratedValue
18-
private int id;
10+
public class EventCategory extends AbstractEntity {
1911

2012
@Size(min=3, message="Name must be at least 3 characters long")
2113
private String name;
@@ -34,26 +26,9 @@ public void setName(String name) {
3426
this.name = name;
3527
}
3628

37-
public int getId() {
38-
return id;
39-
}
40-
4129
@Override
4230
public String toString() {
4331
return name;
4432
}
45-
46-
@Override
47-
public boolean equals(Object o) {
48-
if (this == o) return true;
49-
if (o == null || getClass() != o.getClass()) return false;
50-
EventCategory that = (EventCategory) o;
51-
return id == that.id;
52-
}
53-
54-
@Override
55-
public int hashCode() {
56-
return Objects.hash(id);
57-
}
5833
}
5934

src/main/resources/templates/events/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<!DOCTYPE html>
22
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3-
<head th:replace="fragments :: head"></head>
3+
<head th:replace="~{fragments :: head}"></head>
44
<body class="container">
55

6-
<header th:replace="fragments :: header"></header>
6+
<header th:replace="~{fragments :: header}"></header>
77

88
<table class="table table-striped">
99
<thead>

0 commit comments

Comments
 (0)