java - 带有连接的Spring数据jap多表列
问题描述
我有两个表,user_info 和 user_password。我想在显示userInfo详细信息时直接显示typeName,但是我不想配置这两个实体之间的关系(OneToMany或ManyToOne)。但是它不起作用,谁能帮助我?
@Data
@Entity
@DynamicUpdate
public class UserInfo {// user_info
@Id
private Integer id;
private String name;
private Byte gender;
private String thirdPartId;
private Integer age;
private String telphone;
private String registerMode;
}
@Entity
@Data
public class UserPassword {//user_password
@Id
private Integer id;
private String encryptPassword;
private Integer userId;
}
/**
* This is wish result entity
* @author Mason
* @version v1.0
* @since 2019/1/16
*/
@Data
public class UserInfoPassword {
private Integer id;
private String name;
private Byte gender;
private String thirdPartId;
private Integer age;
private String telphone;
private String registerMode;
private String encryptPassword;
}
@Query(value = "SELECT u.id,u.name,u.gender,u.third_part_id,u.age,u.telphone,u.register_mode,p.encrypt_password FROM user_info u " +
"LEFT JOIN user_password p ON u.id=p.user_id ", nativeQuery = true)
List<UserInfoPassword > queryAll();
此返回类型 UserInfoPassword 将发生异常:org.springframework.core.convert.ConverterNotFoundException:未找到能够从类型 [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] 转换为类型 [com. mason.sell.repository.model.UserInfoPassword]
如果我更改返回对象,然后这样做:
List<Object> userInfoPasswords = userInfoRepository.queryAll();
for (Object o : userInfoPasswords) {
UserInfoPassword uip = (UserInfoPassword) o;
System.out.println(uip);
}
那么就会出现这个异常:
java.lang.ClassCastException:[Ljava.lang.Object;无法转换为 com.mason.sell.repository.model.UserInfoPassword
谁有很好的方法来做多表连接查询???
解决方案
它将返回一个对象数组,因为您没有提供任何映射。
尝试这个:-
List<Object[]> userInfoPasswords = userInfoRepository.queryAll();
for (Object o[] : userInfoPasswords) {
System.out.println("id: " + o[0]); // or call the setter of new UserInfoPassword
System.out.println("name: " + o[1]);
// ... and so on
}
推荐阅读
- python - `from x import x` 究竟是什么意思?
- c# - 在循环中创建对象 C#
- python - 使用 DiscordPy 处理 IndexError 和属性错误
- docker - 如何从容器内部查找容器图像名称/标签
- .net - 将看起来像日期的 DateTime 或 String 类型作为参数传递给 URL / (.net webapi)
- python - 创建 boto3 sts 客户端时出现无效的标头值错误
- blueprint - 在 Popover2 中使用表单输入
- firebase - 即使对经过身份验证的用户使用最简单的规则,Firebase 实时数据库权限也会被拒绝
- r - 仍然无法将 MP4 嵌入闪亮
- sql-server - 以编程方式更改 PowerBI 中的订阅