I'm using spring boot with JPA ( hibernate ) , Before I move to spring boot I was using Spring data JPA with hibernate and the default is to load property values eagerly and to load collections lazily.
in spring boot JPA the following is fetched eagerly by default why ? The roles will be returned inside the user but they should be null
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
That's the example I followed https://medium.com/@gustavo.ponce.ch/spring-boot-spring-mvc-spring-security-mysql-a5d8545d837d
And the application.properties I have edited to the follow :
# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
# ==============================================================
# = Spring Security / Queries for AuthenticationManagerBuilder
# ==============================================================
spring.queries.users-query=select email, password, active from user where email=?
spring.queries.roles-query=select u.email, r.role from user u inner join user_role ur on(u.id=ur.user_id) inner join role r on(ur.role_id=r.id) where u.email=?
=========================================================================== I have tried to add this relation in the user Entity :
@Entity
@Table(name = "user")
@Getter
@Setter
public class User {
@OneToMany(mappedBy = "user")
private List<Test> tests;
}
the tests will be fetched eagerly in this :
@Transactional
@Override
public List<User> getAllUsers() {
List<User>users=userRepository.findAll();
return users;
}
I can't find why this is default instead of lazy loading.