Skip to content

Commit ef2f366

Browse files
committed
Add hibernate to Student
1 parent b3c1984 commit ef2f366

File tree

10 files changed

+94
-10
lines changed

10 files changed

+94
-10
lines changed

src/mooc/main/resources/database/mooc.sql

+11
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,14 @@ CREATE TABLE IF NOT EXISTS domain_events (
5959
ENGINE = InnoDB
6060
DEFAULT CHARSET = utf8mb4
6161
COLLATE = utf8mb4_unicode_ci;
62+
63+
CREATE TABLE IF NOT EXISTS students (
64+
id CHAR(36) NOT NULL,
65+
name VARCHAR(255) NOT NULL,
66+
surname VARCHAR(255) NOT NULL,
67+
email VARCHAR(255) NOT NULL,
68+
PRIMARY KEY (id)
69+
)
70+
ENGINE = InnoDB
71+
DEFAULT CHARSET = utf8mb4
72+
COLLATE = utf8mb4_unicode_ci;

src/mooc/main/tv/codely/mooc/students/domain/Student.java

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ public Student(StudentId id, StudentName name, StudentSurname surname, StudentEm
1515
this.email = email;
1616
}
1717

18+
private Student() {
19+
id = null;
20+
name = null;
21+
surname = null;
22+
email = null;
23+
}
24+
1825
public static Student create(StudentId id, StudentName name, StudentSurname surname, StudentEmail email) {
1926
return new Student(id, name, surname, email);
2027
}

src/mooc/main/tv/codely/mooc/students/domain/StudentEmail.java

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ public final class StudentEmail extends EmailValueObject {
66
public StudentEmail(String email) {
77
super(email);
88
}
9+
10+
private StudentEmail() {
11+
super("");
12+
}
913
}

src/mooc/main/tv/codely/mooc/students/domain/StudentId.java

+3
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ public final class StudentId extends Identifier {
66
public StudentId(String value) {
77
super(value);
88
}
9+
10+
private StudentId() {
11+
}
912
}

src/mooc/main/tv/codely/mooc/students/domain/StudentName.java

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ public final class StudentName extends StringValueObject {
66
public StudentName(String value) {
77
super(value);
88
}
9+
10+
private StudentName() {
11+
super("");
12+
}
913
}

src/mooc/main/tv/codely/mooc/students/domain/StudentSurname.java

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ public final class StudentSurname extends StringValueObject {
66
public StudentSurname(String value) {
77
super(value);
88
}
9+
10+
private StudentSurname() {
11+
super("");
12+
}
913
}

src/mooc/main/tv/codely/mooc/students/infrastructure/InMemoryStudentRepository.java renamed to src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package tv.codely.mooc.students.infrastructure;
1+
package tv.codely.mooc.students.infrastructure.persistence;
22

33
import tv.codely.mooc.students.domain.*;
4-
import tv.codely.shared.domain.Service;
54
import tv.codely.shared.domain.UuidGenerator;
65

76
import java.util.Arrays;
87
import java.util.List;
98

10-
@Service
119
public final class InMemoryStudentRepository implements StudentRepository {
1210
private UuidGenerator generator;
1311

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tv.codely.mooc.students.infrastructure.persistence;
2+
3+
import org.hibernate.SessionFactory;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.transaction.annotation.Transactional;
6+
import tv.codely.mooc.students.domain.Student;
7+
import tv.codely.mooc.students.domain.StudentRepository;
8+
import tv.codely.shared.domain.Service;
9+
import tv.codely.shared.infrastructure.hibernate.HibernateRepository;
10+
11+
import java.util.List;
12+
13+
@Service
14+
@Transactional("mooc-transaction_manager")
15+
public class MySqlStudentRepository extends HibernateRepository<Student> implements StudentRepository {
16+
public MySqlStudentRepository(@Qualifier("mooc-session_factory") SessionFactory sessionFactory) {
17+
super(sessionFactory, Student.class);
18+
}
19+
20+
@Override
21+
public void register(Student student) {
22+
persist(student);
23+
}
24+
25+
@Override
26+
public List<Student> searchAll() {
27+
return all();
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version = "1.0" encoding = "utf-8"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC
3+
"-//Hibernate/Hibernate Mapping DTD//EN"
4+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5+
6+
<hibernate-mapping>
7+
<class name="tv.codely.mooc.students.domain.Student" table="students">
8+
<composite-id name="id" class="tv.codely.mooc.students.domain.StudentId" access="field">
9+
<key-property column="id" name="value" length="36" access="field"/>
10+
</composite-id>
11+
12+
<component name="name" class="tv.codely.mooc.students.domain.StudentName" access="field">
13+
<property name="value" column="name" type="string" access="field"/>
14+
</component>
15+
16+
<component name="surname" class="tv.codely.mooc.students.domain.StudentSurname" access="field">
17+
<property name="value" column="surname" type="string" access="field"/>
18+
</component>
19+
20+
<component name="email" class="tv.codely.mooc.students.domain.StudentEmail" access="field">
21+
<property name="value" column="email" type="string" access="field"/>
22+
</component>
23+
</class>
24+
</hibernate-mapping>

src/shared/main/tv/codely/shared/domain/EmailValueObject.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
public abstract class EmailValueObject {
88
private static final EmailValidator emailValidator = new EmailValidator();
9-
private final String email;
9+
private String value;
1010

11-
public EmailValueObject(String email) {
12-
ensureValidEmail(email);
13-
this.email = email;
11+
public EmailValueObject(String value) {
12+
ensureValidEmail(value);
13+
this.value = value;
1414
}
1515

1616
public String value() {
17-
return email;
17+
return value;
1818
}
1919

2020
private void ensureValidEmail(String value) {
@@ -37,11 +37,11 @@ public boolean equals(Object o) {
3737
return false;
3838
}
3939
EmailValueObject that = (EmailValueObject) o;
40-
return Objects.equals(email, that.email);
40+
return Objects.equals(value, that.value);
4141
}
4242

4343
@Override
4444
public int hashCode() {
45-
return Objects.hash(email);
45+
return Objects.hash(value);
4646
}
4747
}

0 commit comments

Comments
 (0)