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 81654b2

Browse files
author
Rajeev Kumar Singh
committed
Many-to-Many Demo
1 parent ee4d5b3 commit 81654b2

File tree

6 files changed

+233
-1
lines changed

6 files changed

+233
-1
lines changed
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
package com.example.hibernatemanytomanydemo;
22

3+
import com.example.hibernatemanytomanydemo.model.Course;
4+
import com.example.hibernatemanytomanydemo.model.Student;
5+
import com.example.hibernatemanytomanydemo.repository.CourseRepository;
6+
import com.example.hibernatemanytomanydemo.repository.StudentRepository;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.CommandLineRunner;
39
import org.springframework.boot.SpringApplication;
410
import org.springframework.boot.autoconfigure.SpringBootApplication;
511

12+
import java.util.Calendar;
13+
614
@SpringBootApplication
7-
public class HibernateManyToManyDemoApplication {
15+
public class HibernateManyToManyDemoApplication implements CommandLineRunner {
16+
17+
@Autowired
18+
private StudentRepository studentRepository;
19+
20+
@Autowired
21+
private CourseRepository courseRepository;
822

923
public static void main(String[] args) {
1024
SpringApplication.run(HibernateManyToManyDemoApplication.class, args);
1125
}
26+
27+
@Override
28+
public void run(String... args) throws Exception {
29+
studentRepository.deleteAllInBatch();
30+
courseRepository.deleteAllInBatch();
31+
32+
// Create a Student
33+
Student student = studentRepository.save(new Student("Rajeev Kumar Singh", "rajeev@callicoder.com"));
34+
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()));
40+
41+
// Enrol the Student in the Course
42+
student.getCourses().add(course);
43+
studentRepository.save(student);
44+
}
1245
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.example.hibernatemanytomanydemo.model;
2+
3+
import javax.persistence.*;
4+
import javax.validation.constraints.NotNull;
5+
import java.util.Date;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
/**
10+
* Created by rajeevkumarsingh on 22/11/17.
11+
*/
12+
@Entity
13+
@Table(name = "courses")
14+
public class Course {
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Long id;
18+
19+
@NotNull
20+
private String name;
21+
22+
@NotNull
23+
@Temporal(TemporalType.DATE)
24+
private Date startDate;
25+
26+
@NotNull
27+
@Temporal(TemporalType.DATE)
28+
private Date endDate;
29+
30+
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "courses")
31+
private Set<Student> students = new HashSet<>();
32+
33+
public Course() {
34+
35+
}
36+
37+
public Course(String name, Date startDate, Date endDate) {
38+
this.name = name;
39+
this.startDate = startDate;
40+
this.endDate = endDate;
41+
}
42+
43+
public Long getId() {
44+
return id;
45+
}
46+
47+
public void setId(Long id) {
48+
this.id = id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
public Date getStartDate() {
60+
return startDate;
61+
}
62+
63+
public void setStartDate(Date startDate) {
64+
this.startDate = startDate;
65+
}
66+
67+
public Date getEndDate() {
68+
return endDate;
69+
}
70+
71+
public void setEndDate(Date endDate) {
72+
this.endDate = endDate;
73+
}
74+
75+
public Set<Student> getStudents() {
76+
return students;
77+
}
78+
79+
public void setStudents(Set<Student> students) {
80+
this.students = students;
81+
}
82+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.example.hibernatemanytomanydemo.model;
2+
3+
import org.hibernate.validator.constraints.Email;
4+
5+
import javax.persistence.*;
6+
import javax.validation.constraints.NotNull;
7+
import javax.validation.constraints.Size;
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
/**
12+
* Created by rajeevkumarsingh on 22/11/17.
13+
*/
14+
@Entity
15+
@Table(name = "students")
16+
public class Student {
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.AUTO)
19+
private Long id;
20+
21+
@NotNull
22+
@Size(max = 100)
23+
private String name;
24+
25+
@NotNull
26+
@Email
27+
@Size(max = 100)
28+
private String email;
29+
30+
@ManyToMany
31+
@JoinTable(name = "enrolments",
32+
joinColumns = { @JoinColumn(name = "student_id") },
33+
inverseJoinColumns = { @JoinColumn(name = "course_id") })
34+
private Set<Course> courses = new HashSet<>();
35+
36+
public Student() {
37+
38+
}
39+
40+
public Student(String name, String email) {
41+
this.name = name;
42+
this.email = email;
43+
}
44+
45+
public Long getId() {
46+
return id;
47+
}
48+
49+
public void setId(Long id) {
50+
this.id = id;
51+
}
52+
53+
public String getName() {
54+
return name;
55+
}
56+
57+
public void setName(String name) {
58+
this.name = name;
59+
}
60+
61+
public String getEmail() {
62+
return email;
63+
}
64+
65+
public void setEmail(String email) {
66+
this.email = email;
67+
}
68+
69+
public Set<Course> getCourses() {
70+
return courses;
71+
}
72+
73+
public void setCourses(Set<Course> courses) {
74+
this.courses = courses;
75+
}
76+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.hibernatemanytomanydemo.repository;
2+
3+
import com.example.hibernatemanytomanydemo.model.Course;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
/**
8+
* Created by rajeevkumarsingh on 22/11/17.
9+
*/
10+
@Repository
11+
public interface CourseRepository extends JpaRepository<Course, Long> {
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.hibernatemanytomanydemo.repository;
2+
3+
import com.example.hibernatemanytomanydemo.model.Student;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
/**
8+
* Created by rajeevkumarsingh on 22/11/17.
9+
*/
10+
@Repository
11+
public interface StudentRepository extends JpaRepository<Student, Long> {
12+
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
2+
spring.datasource.url=jdbc:mysql://localhost:3306/hibernate_many_to_many_demo?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
3+
spring.datasource.username=root
4+
spring.datasource.password=root
5+
6+
# Hibernate
7+
8+
# The SQL dialect makes Hibernate generate better SQL for the chosen database
9+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
10+
11+
# Hibernate ddl auto (create, create-drop, validate, update)
12+
spring.jpa.hibernate.ddl-auto = update
13+
14+
logging.level.org.hibernate.SQL=DEBUG
15+
logging.level.org.hibernate.type=TRACE

0 commit comments

Comments
(0)

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