Skip to content

Commit 07b7723

Browse files
committed
Add more examples
1 parent e1334bb commit 07b7723

File tree

13 files changed

+323
-55
lines changed

13 files changed

+323
-55
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.github.dndanoff.db;
2+
3+
import org.springframework.boot.CommandLineRunner;
4+
import org.springframework.core.annotation.Order;
5+
import org.springframework.stereotype.Component;
6+
7+
import jakarta.persistence.EntityManager;
8+
import jakarta.persistence.PersistenceContext;
9+
import jakarta.transaction.Transactional;
10+
11+
@Transactional
12+
@Component
13+
@Order(4)
14+
public class PersistOrderDemo implements CommandLineRunner {
15+
16+
@PersistenceContext
17+
private EntityManager em;
18+
19+
@Override
20+
public void run(String... args) {
21+
if (true) {
22+
StudentDemoEntity student = new StudentDemoEntity();
23+
student.setFirstName("Denis");
24+
student.setLastName("Danov");
25+
student.setFacultyNumber("M_TEST@@@");
26+
em.persist(student);
27+
28+
em.remove(student);
29+
30+
StudentDemoEntity newStudent = new StudentDemoEntity();
31+
newStudent.setFirstName("Denis");
32+
newStudent.setLastName("Nikolaev Danov");
33+
newStudent.setFacultyNumber("M_TEST@@@");
34+
em.persist(newStudent);
35+
36+
// THIS IS THE FIX
37+
// student.setLastName("Nikolaev Danov");
38+
// em.persist(student);
39+
}
40+
}
41+
}

spring-data-jpa-entity/src/main/java/com/github/dndanoff/db/SpringTransactionDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SpringTransactionDemo implements CommandLineRunner {
1818

1919
@Override
2020
public void run(String... args) {
21-
if (true) {
21+
if (false) {
2222
StudentDemoEntity student = new StudentDemoEntity();
2323
student.setFirstName("Denis");
2424
student.setLastName("Danov");

spring-data-jpa-entity/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
99
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
1010

1111
# controls schema creation from hibernate
12-
spring.jpa.hibernate.ddl-auto=none
12+
spring.jpa.hibernate.ddl-auto=create
1313
# use names exactly as in the table annotation
1414
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
1515
# How to import data to the DB

spring-data-jpa/src/main/java/com/github/dndanoff/db/SpringJpaDemoRunner.java renamed to spring-data-jpa/src/main/java/com/github/dndanoff/db/CustomJpaMethodsDemo.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
@Component
1111
@Order(2)
12-
public class SpringJpaDemoRunner implements CommandLineRunner {
12+
public class CustomJpaMethodsDemo implements CommandLineRunner {
1313
private StudentRepository repo;
1414

15-
public SpringJpaDemoRunner(StudentRepository repo) {
15+
public CustomJpaMethodsDemo(StudentRepository repo) {
1616
this.repo = repo;
1717
}
1818

@@ -22,8 +22,14 @@ public void run(String... args) {
2222
Student student = new Student("M-test1");
2323
student.setFirstName("Denis");
2424
student.setLastName("Danov");
25+
repo.save(student);
2526

27+
student.setLastName("Nikolaev Danov");
2628
repo.save(student);
29+
30+
System.out.println(repo.customFind("M-test1"));
31+
System.out.println(repo.customNativeFind("M-test1"));
32+
System.out.println(repo.totallyCustomMethod("M-test1"));
2733
}
2834
}
2935
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.dndanoff.db;
2+
3+
import org.springframework.boot.CommandLineRunner;
4+
import org.springframework.core.annotation.Order;
5+
import org.springframework.stereotype.Component;
6+
7+
import com.github.dndanoff.db.entity.dummyrelations.*;
8+
9+
import jakarta.persistence.EntityManager;
10+
import jakarta.persistence.PersistenceContext;
11+
import jakarta.transaction.Transactional;
12+
13+
@Transactional
14+
@Component
15+
@Order(1)
16+
public class RelationsDemo implements CommandLineRunner {
17+
18+
@PersistenceContext
19+
private EntityManager em;
20+
21+
@Override
22+
public void run(String... args) {
23+
if (false) {
24+
DummyFaculty fmi = new DummyFaculty();
25+
fmi.setName("FMI");
26+
27+
DummyStudent student = new DummyStudent("M_TEST");
28+
student.setFirstName("Denis");
29+
student.setLastName("Danov");
30+
fmi.addStudent(student);
31+
32+
em.persist(fmi);
33+
}
34+
}
35+
}

spring-data-jpa/src/main/java/com/github/dndanoff/db/RelationsDemoRunner.java

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

spring-data-jpa/src/main/java/com/github/dndanoff/db/entity/Faculty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Faculty {
2020
private Long id;
2121
@Column(length = 255, nullable = false, unique = true)
2222
private String name;
23-
@OneToMany(mappedBy = "faculty", cascade = CascadeType.ALL, orphanRemoval = true)
23+
@OneToMany(cascade = CascadeType.ALL, mappedBy = "faculty", orphanRemoval = true)
2424
private Set<Student> students = new HashSet<>();
2525

2626
public void addStudent(Student student) {

spring-data-jpa/src/main/java/com/github/dndanoff/db/entity/Student.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public String toString() {
6060
firstName,
6161
lastName,
6262
facultyNumber,
63-
created.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
63+
created != null ? created.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) : null,
6464
deleted != null ? deleted.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) : null);
6565
}
6666

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.dndanoff.db.entity.dummyrelations;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import jakarta.persistence.CascadeType;
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.GenerationType;
11+
import jakarta.persistence.Id;
12+
import jakarta.persistence.JoinColumn;
13+
import jakarta.persistence.OneToMany;
14+
import jakarta.persistence.Table;
15+
16+
@Entity
17+
@Table(name = "DUMMY_FACULTIES")
18+
public class DummyFaculty {
19+
@Id
20+
@GeneratedValue(strategy = GenerationType.IDENTITY)
21+
private Long id;
22+
@Column(length = 255, nullable = false, unique = true)
23+
private String name;
24+
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
25+
private Set<DummyStudent> students = new HashSet<>();
26+
27+
public void addStudent(DummyStudent student) {
28+
students.add(student);
29+
}
30+
31+
public void removeStudent(DummyStudent student) {
32+
students.remove(student);
33+
}
34+
35+
public Long getId() {
36+
return id;
37+
}
38+
39+
public void setId(Long id) {
40+
this.id = id;
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
51+
public Set<DummyStudent> getStudents() {
52+
return new HashSet<>(students);
53+
}
54+
55+
public void setStudents(Set<DummyStudent> students) {
56+
if (students != null) {
57+
this.students = students;
58+
}
59+
}
60+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.github.dndanoff.db.entity.dummyrelations;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.format.DateTimeFormatter;
5+
6+
import jakarta.persistence.Column;
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.GeneratedValue;
9+
import jakarta.persistence.GenerationType;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.Index;
12+
import jakarta.persistence.ManyToOne;
13+
import jakarta.persistence.Table;
14+
import jakarta.persistence.Transient;
15+
16+
@Entity
17+
@Table(name = "DUMMY_STUDENTS", indexes = {
18+
@Index(name = "facultyNum_uniqueIndex", columnList = "facultyNumber", unique = true)
19+
})
20+
public class DummyStudent {
21+
@Id
22+
@GeneratedValue(strategy = GenerationType.IDENTITY)
23+
private Long id;
24+
@Column(length = 255, nullable = false)
25+
private String firstName;
26+
@Column(length = 255, nullable = false)
27+
private String lastName;
28+
@Column(length = 20, unique = true, nullable = false)
29+
private String facultyNumber;
30+
@Transient
31+
private String notPersistedAtribute;
32+
@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
33+
private LocalDateTime created;
34+
private LocalDateTime deleted;
35+
@ManyToOne
36+
private DummyFaculty faculty;
37+
38+
// natural key is passed here so that it has value for new entities
39+
public DummyStudent(String facultyNumber) {
40+
this.facultyNumber = facultyNumber;
41+
}
42+
43+
protected DummyStudent() {
44+
}
45+
46+
public String toString() {
47+
return String.format("ID: %d, FIRST_NAME: %s, LAST_NAME: %s, FACULTY_NUMBER: %s, CREATED: %s, DELETED: %s", id,
48+
firstName,
49+
lastName,
50+
facultyNumber,
51+
created.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
52+
deleted != null ? deleted.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) : null);
53+
}
54+
55+
// There is a natural key so we use it in equals. PAy attention the natural key
56+
// is required in the constructor
57+
@Override
58+
public boolean equals(Object o) {
59+
if (this == o)
60+
return true;
61+
if (!(o instanceof DummyStudent))
62+
return false;
63+
return facultyNumber.equals(((DummyStudent) o).getFacultyNumber());
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return getFacultyNumber().hashCode();
69+
}
70+
71+
public Long getId() {
72+
return id;
73+
}
74+
75+
public void setId(Long id) {
76+
this.id = id;
77+
}
78+
79+
public String getFirstName() {
80+
return firstName;
81+
}
82+
83+
public void setFirstName(String firstName) {
84+
this.firstName = firstName;
85+
}
86+
87+
public String getLastName() {
88+
return lastName;
89+
}
90+
91+
public void setLastName(String lastName) {
92+
this.lastName = lastName;
93+
}
94+
95+
public String getFacultyNumber() {
96+
return facultyNumber;
97+
}
98+
99+
public void setFacultyNumber(String facultyNumber) {
100+
this.facultyNumber = facultyNumber;
101+
}
102+
103+
public LocalDateTime getCreated() {
104+
return created;
105+
}
106+
107+
public void setCreated(LocalDateTime created) {
108+
this.created = created;
109+
}
110+
111+
public LocalDateTime getDeleted() {
112+
return deleted;
113+
}
114+
115+
public void setDeleted(LocalDateTime deleted) {
116+
this.deleted = deleted;
117+
}
118+
}

0 commit comments

Comments
 (0)