I am working on a spring-data-rest module, but there is a problem in the query process: I defined the roles attribute in the User class, added @ManyToMany annotation, and printed relevant sql about the Role during execution, but there was no relevant information about the Role in response. How should I query my role information as well?
Here is my code: user class:
@Entity(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToMany(targetEntity = Role.class,cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
@JoinTable(name = "user_role",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}
)
private Set<Role> roles = new HashSet<>();
}
role class:
@Entity(name = "role")
public class Role implements Serializable {
@Id
private Long id;
@ManyToMany(mappedBy = "roles",cascade = CascadeType.MERGE)
private Set<User> users = new HashSet<>();
UserRepository class:
@RepositoryRestController
public interface UserRepository extends JpaRepository<User,Long>, JpaSpecificationExecutor<User> {
@RequestMapping(value = "findByUsername",method = RequestMethod.GET)
User findByUsername(@RequestParam String username);
}
this is my response in postman: enter image description here
so,why can't I get the role information in user?