Skip to content

Commit 8dafe0a

Browse files
committed
refactor: change fetch type to EAGER for relationships in entities and enable async processing in EmailService
1 parent f0cb141 commit 8dafe0a

7 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/main/java/com/pedro/sphynx/SphynxApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import org.springframework.boot.SpringApplication;
66
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.scheduling.annotation.EnableAsync;
78

89
@SpringBootApplication
10+
@EnableAsync
911
public class SphynxApplication {
1012

1113
public static void main(String[] args) {

src/main/java/com/pedro/sphynx/entities/Access.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public class Access {
1919
@GeneratedValue(strategy = GenerationType.IDENTITY)
2020
private Long id;
2121

22-
@ManyToOne(fetch = FetchType.LAZY)
22+
@ManyToOne(fetch = FetchType.EAGER)
2323
@JoinColumn(name = "user_id")
2424
private User user;
2525

26-
@ManyToOne(fetch = FetchType.LAZY)
26+
@ManyToOne(fetch = FetchType.EAGER)
2727
@JoinColumn(name = "local_id")
2828
private Local local;
2929

30-
@ManyToOne(fetch = FetchType.LAZY)
30+
@ManyToOne(fetch = FetchType.EAGER)
3131
@JoinColumn(name = "unit_id")
3232
private Unit unit;
3333

src/main/java/com/pedro/sphynx/entities/Group.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ public class Group {
2929
@JoinTable(name = "locals_groups", joinColumns = @JoinColumn(name = "group_id"), inverseJoinColumns = @JoinColumn(name = "local_id"))
3030
private Set<Local> locals;
3131

32-
@ManyToOne(fetch = FetchType.LAZY)
32+
@ManyToOne(fetch = FetchType.EAGER)
3333
@JoinColumn(name = "user_id")
3434
private User user;
3535

36-
@ManyToOne(fetch = FetchType.LAZY)
36+
@ManyToOne(fetch = FetchType.EAGER)
3737
@JoinColumn(name = "unit_id")
3838
private Unit unit;
3939

40-
@ManyToMany
40+
@ManyToMany(fetch = FetchType.EAGER)
4141
@JoinTable(name = "week_days_groups", joinColumns = @JoinColumn(name = "group_id"), inverseJoinColumns = @JoinColumn(name = "week_day_id"))
4242
private Set<WeekDay> weekDays;
4343

src/main/java/com/pedro/sphynx/entities/Unit.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ public class Unit {
2626
private LocalDateTime dtcreate;
2727
private LocalDateTime dtupdate;
2828

29-
@OneToMany(mappedBy = "unit", fetch = FetchType.LAZY)
29+
@OneToMany(mappedBy = "unit", fetch = FetchType.EAGER)
3030
private List<User> users;
3131

32-
@OneToMany(mappedBy = "unit", fetch = FetchType.LAZY)
32+
@OneToMany(mappedBy = "unit", fetch = FetchType.EAGER)
3333
private List<Local> locals;
3434

35-
@OneToMany(mappedBy = "unit", fetch = FetchType.LAZY)
35+
@OneToMany(mappedBy = "unit", fetch = FetchType.EAGER)
3636
private List<Group> groups;
3737

38-
@OneToMany(mappedBy = "unit", fetch = FetchType.LAZY)
38+
@OneToMany(mappedBy = "unit", fetch = FetchType.EAGER)
3939
private List<Access> accesses;
4040

4141
public Unit(UnitDataInput data){

src/main/java/com/pedro/sphynx/entities/User.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ public class User implements UserDetails {
3939

4040
private String password;
4141

42-
@ManyToOne(fetch = FetchType.LAZY)
42+
@ManyToOne(fetch = FetchType.EAGER)
4343
@JoinColumn(name = "unit_id")
4444
private Unit unit;
4545

46-
@ManyToOne(fetch = FetchType.LAZY)
46+
@ManyToOne(fetch = FetchType.EAGER)
4747
@JoinColumn(name = "group_id")
4848
private Group group;
4949

50-
@ManyToOne(fetch = FetchType.LAZY)
50+
@ManyToOne(fetch = FetchType.EAGER)
5151
@JoinColumn(name = "user_id")
5252
private User userCreator;
5353

54-
@ManyToMany(fetch = FetchType.LAZY)
54+
@ManyToMany(fetch = FetchType.EAGER)
5555
@JoinTable(name = "permission_menus_users", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "permission_menu_id"))
5656
private List<PermissionMenu> permissionMenus;
5757

src/main/java/com/pedro/sphynx/services/EmailService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.mail.SimpleMailMessage;
55
import org.springframework.mail.javamail.JavaMailSender;
6+
import org.springframework.scheduling.annotation.Async;
67
import org.springframework.stereotype.Service;
78

89
@Service
@@ -11,6 +12,7 @@ public class EmailService {
1112
@Autowired
1213
private JavaMailSender emailSender;
1314

15+
@Async
1416
public void sendSimpleMessage(String to, String subject, String text) {
1517
SimpleMailMessage message = new SimpleMailMessage();
1618
message.setFrom("pedrohvidals@gmail.com");

src/main/java/com/pedro/sphynx/services/UserService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1010
import org.springframework.stereotype.Service;
11+
import org.springframework.transaction.annotation.Transactional;
1112

1213
import com.pedro.sphynx.dtos.auth.UserDataComplete;
1314
import com.pedro.sphynx.dtos.auth.UserDataRegisterInput;
@@ -44,6 +45,7 @@ public class UserService {
4445

4546
private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(12);
4647

48+
@Transactional
4749
public UserDataComplete create(UserDataRegisterInput data, User loggedUser) {
4850
if (userRepository.existsByUser(data.user())) {
4951
throw new EntityExistsException("User already exists");
@@ -129,12 +131,14 @@ public UserDataComplete create(UserDataRegisterInput data, User loggedUser) {
129131
return new UserDataComplete(userCreated);
130132
}
131133

134+
@Transactional
132135
public UserDataComplete getById(Long id) {
133136
User userEntity = userRepository.findById(id)
134137
.orElseThrow(() -> new EntityExistsException("User not found"));
135138
return new UserDataComplete(userEntity);
136139
}
137140

141+
@Transactional
138142
public List<UserDataComplete> getAll(User loggedUser) {
139143
List<UserDataComplete> listUsers;
140144

@@ -156,6 +160,7 @@ public List<UserDataComplete> getAll(User loggedUser) {
156160
return listUsers;
157161
}
158162

163+
@Transactional
159164
public void delete(Long id) {
160165
if (!userRepository.existsById(id)) {
161166
throw new EntityExistsException("User not found");
@@ -164,6 +169,7 @@ public void delete(Long id) {
164169
userRepository.deleteById(id);
165170
}
166171

172+
@Transactional
167173
public UserDataComplete update(UserDataRegisterInput data, Long id){
168174
if(!userRepository.existsById(id)) {
169175
throw new EntityNotFoundException(messages.getString("error.idDontExists"));

0 commit comments

Comments
 (0)