Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从spring boot jpa中的两个表中获取数据_Java_Spring_Spring Boot_Jpa_Spring Data Jpa - Fatal编程技术网

Java 如何从spring boot jpa中的两个表中获取数据

Java 如何从spring boot jpa中的两个表中获取数据,java,spring,spring-boot,jpa,spring-data-jpa,Java,Spring,Spring Boot,Jpa,Spring Data Jpa,我有用户信息和主题如下表: 包含列的用户信息表: id, username, userImage, token, role id, topicId, title, details, dayPosted, username, userImage 包含列的主题表: id, username, userImage, token, role id, topicId, title, details, dayPosted, username, userImage 在用户登录时,我想从主题表和角色表中

我有
用户信息
主题
如下表:

包含列的用户信息表:

id, username, userImage, token, role
id, topicId, title, details, dayPosted, username, userImage
包含列的主题表:

id, username, userImage, token, role
id, topicId, title, details, dayPosted, username, userImage
在用户登录时,我想从
主题
表和
角色
表中获取信息

现在我得到的数据是这样的,但这不包括角色信息

@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);
    }
@RequestMapping(path=“/get\u data\u on\u login”,method=RequestMethod.get)
公共响应在登录时获取数据(@RequestParam(value=“username”)字符串username)引发异常{
List topicBean=topicService.findAllTopics();
返回新的ResponseEntity(topicBean,HttpStatus.OK);
}

如何从
user\u-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 );
}
您将得到一个JSON响应,如下所示:

{ "topics": [{topic1},{topic2}], "roles": [{role1},{role2}] }

像这样的东西有什么问题

@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 );
}
您将得到一个JSON响应,如下所示:

{ "topics": [{topic1},{topic2}], "roles": [{role1},{role2}] }

我得到错误:
org.springframework.beans.factory.UnsatifiedPendencyException:org.springframework.beans.factory.BeanCreationException:创建名为“userInfoRepository”的bean时出错:调用init方法失败;嵌套异常为org.springframework.data.mapping.PropertyReferenceException:未找到UserInfo类型的属性findAllRoles
我有
@Column(name=“role”)私有字符串role=“role\u USER”
UserInfo
实体classInside DB表中,我的列为
角色
。我应该更改它吗?
角色
?修复如下:
@RequestMapping(path=“/get\u data\u on\u login”,method=RequestMethod.get)公共响应在\u login(@RequestParam(value=“username”)字符串用户名)上获取数据抛出异常{List topics=topicService.findAllTopics();UserInfo role=userInfoService.findRoleByUsername(username);返回新的ResponseEntity(新的登录数据表(主题,role.getRole(),role.getUserImage()),HttpStatus.OK);}
对,您需要将我的示例代码应用到您的特定用例中,但似乎您已经解决了这个问题。祝你好运。我收到错误:
org.springframework.beans.factory.unsatifiedDependencyException:org.springframework.beans.factory.BeanCreationException:创建名为“UserInformationPository”的bean时出错:调用init方法失败;嵌套异常为org.springframework.data.mapping.PropertyReferenceException:未找到UserInfo类型的属性findAllRoles
我有
@Column(name=“role”)私有字符串role=“role\u USER”
UserInfo
实体classInside DB表中,我的列为
角色
。我应该更改它吗?
角色
?修复如下:
@RequestMapping(path=“/get\u data\u on\u login”,method=RequestMethod.get)公共响应在\u login(@RequestParam(value=“username”)字符串用户名)上获取数据抛出异常{List topics=topicService.findAllTopics();UserInfo role=userInfoService.findRoleByUsername(username);返回新的ResponseEntity(新的登录数据表(主题,role.getRole(),role.getUserImage()),HttpStatus.OK);}
对,您需要将我的示例代码应用到您的特定用例中,但似乎您已经解决了这个问题。祝你好运