如何在 java spring 中将对象转换为字符串
How to convert object to string in java spring
我想按 bedType 在 spring 数据 jpa 中搜索。但是,bedType 不是字符串。不是 String bedType,而是 BedType bedType(Object)。 这是我的存储库
@Query("select a,b.bedType,b.roomCategory from RoomDetail a left outer join RoomMaster b on a.roomId = b.id where lower(b.bedType) like %:bedType%")
<Page>RoomDetail findByBedType(
@Param("bedType") BedType bedType,
Pageable paging);
FindRoomStatus.Java
public class FindRoomStatus {
private BedType bedType;
public BedType getBedType() {
return bedType;
}
public void setBedType(BedType bedType) {
this.bedType = bedType;
}
在我的控制器中,出现错误
String cannot be a converted to BedType
data = roomDetailRepository.findByBedType(findRoomStatus.getBedType().toString(), paging);
这里是BedType.Java
public class BedType implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "bed_type_seq")
@SequenceGenerator(name = "bed_type_seq", sequenceName = "bed_type_id_bed_type_seq", allocationSize = 1, initialValue = 1)
private int id;
@Column(name = "bed_type_name", length = 20)
private String bedTypeName;
@JsonIgnore
@Column(name = "status", length = 10)
private String status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBedTypeName() {
return bedTypeName;
}
public void setBedTypeName(String bedTypeName) {
this.bedTypeName = bedTypeName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
列出房间状态,在此列表中,我想按床型查找
{
"id": 105,
"roomId": 43,
"floor": "1",
"roomNumber": "001",
"description": "Normal",
"status": "Vacant Clean"
},
{
"id": 11,
"bedTypeName": "King size"
},
{
"id": 39,
"categoryName": "President Suite"
}
您在查询中使用了 LIKE
关键字,但您接受 BedType
对象作为查询中的参数。您正在从控制器发送 String
作为参数。这就是问题。 toString
方法将给出对象的字符串表示形式。
你可以做的是,将参数更改为 String
,它接受 bedTypeName
,例如:
@Query("select a,b.bedType,b.roomCategory from RoomDetail a left outer
join RoomMaster b on a.roomId = b.id where lower(b.bedType.bedTypeName) like
%:bedTypeName%")
<Page>RoomDetail findByBedType(
@Param("bedTypeName") String bedTypeName,
Pageable paging);
从控制器,
data = roomDetailRepository.findByBedType(findRoomStatus.getBedType().getBedTypeName(),
paging);
我想按 bedType 在 spring 数据 jpa 中搜索。但是,bedType 不是字符串。不是 String bedType,而是 BedType bedType(Object)。 这是我的存储库
@Query("select a,b.bedType,b.roomCategory from RoomDetail a left outer join RoomMaster b on a.roomId = b.id where lower(b.bedType) like %:bedType%")
<Page>RoomDetail findByBedType(
@Param("bedType") BedType bedType,
Pageable paging);
FindRoomStatus.Java
public class FindRoomStatus {
private BedType bedType;
public BedType getBedType() {
return bedType;
}
public void setBedType(BedType bedType) {
this.bedType = bedType;
}
在我的控制器中,出现错误
String cannot be a converted to BedType
data = roomDetailRepository.findByBedType(findRoomStatus.getBedType().toString(), paging);
这里是BedType.Java
public class BedType implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "bed_type_seq")
@SequenceGenerator(name = "bed_type_seq", sequenceName = "bed_type_id_bed_type_seq", allocationSize = 1, initialValue = 1)
private int id;
@Column(name = "bed_type_name", length = 20)
private String bedTypeName;
@JsonIgnore
@Column(name = "status", length = 10)
private String status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBedTypeName() {
return bedTypeName;
}
public void setBedTypeName(String bedTypeName) {
this.bedTypeName = bedTypeName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
列出房间状态,在此列表中,我想按床型查找
{
"id": 105,
"roomId": 43,
"floor": "1",
"roomNumber": "001",
"description": "Normal",
"status": "Vacant Clean"
},
{
"id": 11,
"bedTypeName": "King size"
},
{
"id": 39,
"categoryName": "President Suite"
}
您在查询中使用了 LIKE
关键字,但您接受 BedType
对象作为查询中的参数。您正在从控制器发送 String
作为参数。这就是问题。 toString
方法将给出对象的字符串表示形式。
你可以做的是,将参数更改为 String
,它接受 bedTypeName
,例如:
@Query("select a,b.bedType,b.roomCategory from RoomDetail a left outer
join RoomMaster b on a.roomId = b.id where lower(b.bedType.bedTypeName) like
%:bedTypeName%")
<Page>RoomDetail findByBedType(
@Param("bedTypeName") String bedTypeName,
Pageable paging);
从控制器,
data = roomDetailRepository.findByBedType(findRoomStatus.getBedType().getBedTypeName(),
paging);