![](/img/trans.png)
[英]How do I get “no cascading”-behaviour using spring-boot, spring-data-jpa and hibernate?
[英]How do I get data from two tables in spring boot jpa
我有user_info
和topics
这样的表:
带有列的user_info表:
id, username, userImage, token, role
带列的主题表:
id, topicId, title, details, dayPosted, username, userImage
在用户登录时,我想从topics
表中获取信息,并从user_role
表中获取role
。
目前,我正在获取这样的数据,但这不包括角色信息。
@RequestMapping(path = "/get_data_on_login", method = RequestMethod.GET)
public ResponseEntity get_data_on_login(@RequestParam(value="username") String username) throws Exception {
List<TopicBean> topicBean = topicService.findAllTopics();
return new ResponseEntity(topicBean, HttpStatus.OK);
}
我如何从user_role
表中获取用户角色以及上述数据?
诸如此类的问题是什么?
@RequestMapping(path = "/get_data_on_login", method = RequestMethod.GET)
public ResponseEntity get_data_on_login(
@RequestParam(value="username") String userName) throws Exception {
List<TopicBean> topics = topicService.findAllTopics( userName );
List<UserRoleBean> roles = roleService.findAllRoles( userName );
return new ResponseEntity( new LoginData( topics, roles ), HttpStatus.OK );
}
您的LoginData
类将是:
public class LoginData {
private final List<TopicBean> topics;
private final List<UserRoleBean> roles;
public LoginData(List<TopicBean> topics, List<UserRoleBean> roles) {
this.topics = topics;
this.roles = roles;
}
public List<TopicBean> getTopics() { return topics; }
public List<UserRoleBean> getRoles() { return roles; } }
}
您将获得一个类似于以下内容的JSON响应:
{ "topics": [{topic1},{topic2}], "roles": [{role1},{role2}] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.