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

Commit 3b4a927

Browse files
author
Rajeev Kumar Singh
committed
New Example
1 parent fe1b2ad commit 3b4a927

File tree

7 files changed

+214
-180
lines changed

7 files changed

+214
-180
lines changed
Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.example.hibernate;
22

3-
import com.example.hibernate.model.Course;
4-
import com.example.hibernate.model.Student;
5-
import com.example.hibernate.repository.CourseRepository;
6-
import com.example.hibernate.repository.StudentRepository;
3+
import com.example.hibernate.model.Post;
4+
import com.example.hibernate.model.Tag;
5+
import com.example.hibernate.repository.PostRepository;
6+
import com.example.hibernate.repository.TagRepository;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.CommandLineRunner;
99
import org.springframework.boot.SpringApplication;
@@ -15,31 +15,44 @@
1515
public class HibernateManyToManyDemoApplication implements CommandLineRunner {
1616

1717
@Autowired
18-
private StudentRepositorystudentRepository;
18+
private TagRepositorytagRepository;
1919

2020
@Autowired
21-
private CourseRepositorycourseRepository;
21+
private PostRepositorypostRepository;
2222

2323
public static void main(String[] args) {
2424
SpringApplication.run(HibernateManyToManyDemoApplication.class, args);
2525
}
2626

2727
@Override
2828
public void run(String... args) throws Exception {
29-
studentRepository.deleteAllInBatch();
30-
courseRepository.deleteAllInBatch();
29+
// Cleanup the tables
30+
postRepository.deleteAllInBatch();
31+
tagRepository.deleteAllInBatch();
3132

32-
// Create a Student
33-
Student student = studentRepository.save(new Student("Rajeev Kumar Singh", "rajeev@callicoder.com"));
33+
// =======================================
3434

35-
// Create a Course
36-
Calendar courseStartDate = Calendar.getInstance();
37-
Calendar courseEndDate = Calendar.getInstance();
38-
courseEndDate.add(Calendar.MONTH, 6);
39-
Course course = courseRepository.save(new Course("Algorithms 101", courseStartDate.getTime(), courseEndDate.getTime()));
35+
// Create a Post
36+
Post post = new Post("Hibernate Many to Many Example with Spring Boot",
37+
"Learn how to map a many to many relationship using hibernate",
38+
"Entire Post content with Sample code");
39+
40+
// Create two tags
41+
Tag tag1 = new Tag("Spring Boot");
42+
Tag tag2 = new Tag("Hibernate");
43+
44+
45+
// Add tag references in the post
46+
post.getTags().add(tag1);
47+
post.getTags().add(tag2);
48+
49+
// Add post reference in the tags
50+
tag1.getPosts().add(post);
51+
tag2.getPosts().add(post);
52+
53+
postRepository.save(post);
54+
55+
// =======================================
4056

41-
// Enrol the Student in the Course
42-
student.getCourses().add(course);
43-
studentRepository.save(student);
4457
}
4558
}

‎hibernate-many-to-many-demo/src/main/java/com/example/hibernate/model/Course.java‎

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.example.hibernate.model;
2+
3+
import javax.persistence.*;
4+
import javax.validation.constraints.NotNull;
5+
import javax.validation.constraints.Size;
6+
import java.util.Date;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
/**
11+
* Created by rajeevkumarsingh on 23/11/17.
12+
*/
13+
@Entity
14+
@Table(name = "posts")
15+
public class Post {
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.AUTO)
18+
private Long id;
19+
20+
@NotNull
21+
@Size(max = 100)
22+
private String title;
23+
24+
@NotNull
25+
@Size(max = 250)
26+
private String description;
27+
28+
@NotNull
29+
@Lob
30+
private String content;
31+
32+
@NotNull
33+
@Temporal(TemporalType.TIMESTAMP)
34+
private Date postedAt = new Date();
35+
36+
@NotNull
37+
@Temporal(TemporalType.TIMESTAMP)
38+
private Date lastUpdatedAt = new Date();
39+
40+
41+
@ManyToMany(fetch = FetchType.LAZY,
42+
cascade = {
43+
CascadeType.PERSIST,
44+
CascadeType.MERGE
45+
})
46+
@JoinTable(name = "post_tags",
47+
joinColumns = { @JoinColumn(name = "post_id") },
48+
inverseJoinColumns = { @JoinColumn(name = "tag_id") })
49+
private Set<Tag> tags = new HashSet<>();
50+
51+
52+
public Post() {
53+
54+
}
55+
56+
public Post(String title, String description, String content) {
57+
this.title = title;
58+
this.description = description;
59+
this.content = content;
60+
}
61+
62+
public Long getId() {
63+
return id;
64+
}
65+
66+
public void setId(Long id) {
67+
this.id = id;
68+
}
69+
70+
public String getTitle() {
71+
return title;
72+
}
73+
74+
public void setTitle(String title) {
75+
this.title = title;
76+
}
77+
78+
public String getDescription() {
79+
return description;
80+
}
81+
82+
public void setDescription(String description) {
83+
this.description = description;
84+
}
85+
86+
public String getContent() {
87+
return content;
88+
}
89+
90+
public void setContent(String content) {
91+
this.content = content;
92+
}
93+
94+
public Date getPostedAt() {
95+
return postedAt;
96+
}
97+
98+
public void setPostedAt(Date postedAt) {
99+
this.postedAt = postedAt;
100+
}
101+
102+
public Date getLastUpdatedAt() {
103+
return lastUpdatedAt;
104+
}
105+
106+
public void setLastUpdatedAt(Date lastUpdatedAt) {
107+
this.lastUpdatedAt = lastUpdatedAt;
108+
}
109+
110+
public Set<Tag> getTags() {
111+
return tags;
112+
}
113+
114+
public void setTags(Set<Tag> tags) {
115+
this.tags = tags;
116+
}
117+
}

‎hibernate-many-to-many-demo/src/main/java/com/example/hibernate/model/Student.java‎

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /