Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2ab4346

Browse files
Step 4. Adjust JPA model
1 parent1d96c2d commit2ab4346

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
packageio.amplicode.blog.model;
2+
3+
importjakarta.persistence.*;
4+
importorg.hibernate.proxy.HibernateProxy;
5+
6+
importjava.util.Objects;
7+
8+
@Entity
9+
@Table(name ="comments")
10+
publicclassCommentextendsBaseEntity {
11+
@Column(name ="text",nullable =false)
12+
privateStringtext;
13+
14+
@ManyToOne(fetch =FetchType.LAZY,optional =false)
15+
@JoinColumn(name ="author_id",nullable =false)
16+
privateUserauthor;
17+
18+
publicUsergetAuthor() {
19+
returnauthor;
20+
}
21+
22+
publicvoidsetAuthor(Userauthor) {
23+
this.author =author;
24+
}
25+
26+
publicStringgetText() {
27+
returntext;
28+
}
29+
30+
publicvoidsetText(Stringtext) {
31+
this.text =text;
32+
}
33+
34+
@Override
35+
publicfinalbooleanequals(Objecto) {
36+
if (this ==o)returntrue;
37+
if (o ==null)returnfalse;
38+
Class<?>oEffectiveClass =oinstanceofHibernateProxy
39+
? ((HibernateProxy)o).getHibernateLazyInitializer()
40+
.getPersistentClass()
41+
:o.getClass();
42+
Class<?>thisEffectiveClass =thisinstanceofHibernateProxy
43+
? ((HibernateProxy)this).getHibernateLazyInitializer()
44+
.getPersistentClass()
45+
:this.getClass();
46+
if (thisEffectiveClass !=oEffectiveClass)returnfalse;
47+
Commentcomment = (Comment)o;
48+
returngetId() !=null &&Objects.equals(getId(),comment.getId());
49+
}
50+
51+
@Override
52+
publicfinalinthashCode() {
53+
returnthisinstanceofHibernateProxy
54+
? ((HibernateProxy)this).getHibernateLazyInitializer()
55+
.getPersistentClass()
56+
.hashCode()
57+
:getClass().hashCode();
58+
}
59+
}

‎flyway-and-spring-boot/blog/src/main/java/io/amplicode/blog/model/Post.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
importjakarta.persistence.*;
44

55
importjava.time.Instant;
6+
importjava.util.LinkedHashSet;
7+
importjava.util.Set;
68

79
@Entity
810
@Table(name ="posts")
@@ -21,6 +23,20 @@ public class Post extends BaseEntity {
2123
@JoinColumn(name ="author_id",nullable =false)
2224
privateUserauthor;
2325

26+
@ManyToMany
27+
@JoinTable(name ="posts_comments",
28+
joinColumns =@JoinColumn(name ="post_id"),
29+
inverseJoinColumns =@JoinColumn(name ="comments_id"))
30+
privateSet<Comment>comments =newLinkedHashSet<>();
31+
32+
publicSet<Comment>getComments() {
33+
returncomments;
34+
}
35+
36+
publicvoidsetComments(Set<Comment>comments) {
37+
this.comments =comments;
38+
}
39+
2440
publicStringgetTitle() {
2541
returntitle;
2642
}

‎flyway-and-spring-boot/blog/src/main/java/io/amplicode/blog/model/User.java‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,12 @@ public class User extends BaseEntity {
1818
@OneToMany(mappedBy ="author")
1919
privateSet<Post>posts =newLinkedHashSet<>();
2020

21-
@Column(name ="email")
21+
@Column(name ="email",nullable =false)
2222
privateStringemail;
2323

24-
@Column(name ="fax")
25-
privateStringfax;
26-
2724
@Column(name ="last_activity")
2825
privateInstantlastActivity;
2926

30-
publicStringgetFax() {
31-
returnfax;
32-
}
33-
34-
publicvoidsetFax(Stringfax) {
35-
this.fax =fax;
36-
}
37-
3827
publicInstantgetLastActivity() {
3928
returnlastActivity;
4029
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp