Skip to content

Commit fffb965

Browse files
committed
Spring models
Spring repositories
1 parent e4443d8 commit fffb965

File tree

8 files changed

+191
-52
lines changed

8 files changed

+191
-52
lines changed

src/main/java/attestation_2/hibernate/orms/tests/TestTests.java

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

src/main/java/attestation_2/hibernate/orms/tests/models/StudentAnswer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ public class StudentAnswer {
2121
@JoinColumn(name = "user_id", nullable = false)
2222
private User student;
2323

24-
@NotNull
2524
@OneToOne(fetch = FetchType.LAZY)
26-
@JoinColumn(name = "answer_id", nullable = false)
25+
@JoinColumn(name = "answer_id")
2726
private Answer answer;
2827

2928
public StudentAnswer() {}

src/main/java/attestation_2/hibernate/orms/users_autos/TestUsersAutos.java

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

src/main/java/attestation_2/hibernate/resources/users_autos.cfg.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
1111
<property name="show_sql">true</property>
1212
<property name="connection_pool_size">100</property>
13-
<property name="hbm2ddl.auto">none</property>
13+
<property name="hbm2ddl.auto">create</property>
1414
</session-factory>
1515
</hibernate-configuration>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package tests;
2+
3+
4+
import attestation_2.hibernate.orms.tests.services.RoleService;
5+
import attestation_2.hibernate.orms.tests.services.StudentAnswerService;
6+
import attestation_2.hibernate.orms.tests.services.TestListService;
7+
import attestation_2.hibernate.orms.tests.services.UserService;
8+
import attestation_2.hibernate.utils.HibernateSession;
9+
import org.hibernate.cfg.Configuration;
10+
import attestation_2.hibernate.orms.tests.models.*;
11+
12+
public class MainTests {
13+
private static void init() {
14+
HibernateSession.setConf(new Configuration().configure("tests.cfg.xml")
15+
.addAnnotatedClass(StudentAnswer.class)
16+
.addAnnotatedClass(User.class)
17+
.addAnnotatedClass(Group.class)
18+
.addAnnotatedClass(Answer.class)
19+
.addAnnotatedClass(TestList.class)
20+
.addAnnotatedClass(Schedule.class)
21+
.addAnnotatedClass(Question.class)
22+
.addAnnotatedClass(Test.class)
23+
.addAnnotatedClass(Role.class)
24+
.addAnnotatedClass(Subject.class)
25+
);
26+
}
27+
28+
public static void main(String[] args) {
29+
init();
30+
// Task A
31+
RoleService roleService = new RoleService();
32+
UserService userService = new UserService();
33+
34+
Role teacherRole = new Role("Teacher");
35+
roleService.saveRole(teacherRole);
36+
Role studentRole = new Role("Student");
37+
roleService.saveRole(studentRole);
38+
for (int i = 0; i < 5; i++) {
39+
User stud = new User("stud_" + i,
40+
"ln", "mn",
41+
"studLogin_" + i, "ph");
42+
stud.setRole(studentRole);
43+
userService.saveUser(stud);
44+
}
45+
46+
for (int i = 0; i < 10; i++) {
47+
User teach = new User("teach_" + i,
48+
"ln", "mn",
49+
"teachLogin_" + i, "ph");
50+
teach.setRole(teacherRole);
51+
userService.saveUser(teach);
52+
}
53+
54+
TestListService testListService = new TestListService();
55+
StudentAnswerService studentAnswerService = new StudentAnswerService();
56+
57+
for (User user: userService.findAllUsers()) {
58+
if (user.getRole().getName().equals("Student")) {
59+
TestList testList = new TestList("test_" + user.getFirstName());
60+
testListService.saveTestList(testList);
61+
StudentAnswer studentAnswer = new StudentAnswer();
62+
studentAnswer.setStudent(user);
63+
studentAnswer.setTestList(testList);
64+
studentAnswerService.saveStudentAnswer(studentAnswer);
65+
}
66+
}
67+
68+
// Task B
69+
System.out.println("Teachers:");
70+
for (User user: userService.findAllUsers()) {
71+
if (user.getRole().getId() == teacherRole.getId()) {
72+
System.out.println(user);
73+
}
74+
}
75+
76+
// Task C
77+
System.out.println("Students:");
78+
for (User user: userService.findAllUsers()) {
79+
if (user.getRole().getId() == studentRole.getId()) {
80+
System.out.println(user);
81+
}
82+
}
83+
84+
// Task D
85+
System.out.println("Students:");
86+
for (User user: userService.findAllUsers()) {
87+
if (user.getRole().getId() == studentRole.getId()) {
88+
// Лень
89+
}
90+
}
91+
92+
// Task E
93+
// Лень
94+
}
95+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package users_autos;
2+
3+
import attestation_2.hibernate.orms.users_autos.services.AutoService;
4+
import attestation_2.hibernate.orms.users_autos.services.UserService;
5+
import attestation_2.hibernate.utils.HibernateSession;
6+
import org.hibernate.cfg.Configuration;
7+
import attestation_2.hibernate.orms.users_autos.models.*;
8+
9+
10+
public class MainUsersAutos {
11+
private static void init() {
12+
HibernateSession.setConf(new Configuration().configure("tests.cfg.xml")
13+
.addAnnotatedClass(User.class)
14+
.addAnnotatedClass(Auto.class)
15+
);
16+
17+
UserService userService = new UserService();
18+
AutoService autoService = new AutoService();
19+
20+
for (int i = 0; i < 10; i++) {
21+
User user = new User("name_" + i, 18 + i);
22+
userService.saveUser(user);
23+
Auto auto = new Auto("model" + i, "green");
24+
auto.setUser(user);
25+
autoService.saveAuto(auto);
26+
}
27+
}
28+
29+
public static void main(String[] args) {
30+
init();
31+
}
32+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package attestation_2.spring.controllers;
2+
3+
import attestation_2.spring.exceptions.ResourceNotFoundException;
4+
import attestation_2.spring.models.Role;
5+
import attestation_2.spring.models.Role;
6+
import attestation_2.spring.repsoitories.RoleRepository;
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.Pageable;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.*;
11+
12+
import javax.validation.Valid;
13+
14+
@RestController
15+
public class RoleController {
16+
private final RoleRepository roleRepository;
17+
18+
public RoleController(RoleRepository roleRepository) {
19+
this.roleRepository = roleRepository;
20+
}
21+
22+
@GetMapping("/roles")
23+
public Page<Role> getRoles(Pageable pageable) {
24+
return roleRepository.findAll(pageable);
25+
}
26+
27+
@GetMapping("/roles/{roleId}")
28+
public Role getRoles(@PathVariable int roleId) {
29+
return roleRepository.findById(roleId)
30+
.orElseThrow(() -> new ResourceNotFoundException("Role not found with id " + roleId));
31+
}
32+
33+
@PostMapping("/roles")
34+
public Role createRole(@Valid @RequestBody Role role) {
35+
return roleRepository.save(role);
36+
}
37+
38+
@PutMapping("/roles/{roleId}")
39+
public Role updateRole(@PathVariable int roleId, @Valid @RequestBody Role roleRequest) {
40+
return roleRepository.findById(roleId)
41+
.map(role -> {
42+
role.setName(roleRequest.getName());
43+
return roleRepository.save(role);
44+
}).orElseThrow(() -> new ResourceNotFoundException("Role not found with id " + roleId));
45+
}
46+
47+
@DeleteMapping("/roles/{roleId}")
48+
public ResponseEntity<?> deleteRole(@PathVariable int roleId) {
49+
return roleRepository.findById(roleId)
50+
.map(role -> {
51+
roleRepository.delete(role);
52+
return ResponseEntity.ok().build();
53+
}).orElseThrow(() -> new ResourceNotFoundException("Role not found with id " + roleId));
54+
}
55+
}

src/main/java/attestation_2/spring/models/TestList.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import attestation_2.spring.utils.AuditModel;
44
import com.fasterxml.jackson.annotation.JsonIgnore;
5+
import org.hibernate.annotations.GenericGenerator;
56
import org.hibernate.annotations.OnDelete;
67
import org.hibernate.annotations.OnDeleteAction;
78

@@ -14,7 +15,12 @@
1415
public class TestList extends AuditModel {
1516
@Id
1617
@NotNull
17-
@GeneratedValue(strategy = GenerationType.AUTO)
18+
// @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "pooled-lo")
19+
// @GenericGenerator( name = "pooled-lo",
20+
// strategy = "sequence", parameters = {
21+
// @Parameter(name = "sequence_name", value = "post_sequence"),
22+
// @Parameter(name = "initial_value", value = "1"),
23+
// @Parameter(name = "optimizer", value = "pooled-lo") })
1824
private int id;
1925

2026
@NotNull

0 commit comments

Comments
 (0)