简体   繁体   English

如何在java spring中将对象转换为字符串

[英]How to convert object to string in java spring

I want to search in spring data jpa by bedType.我想通过 BedType 在 spring 数据 jpa 中搜索。 But, bedType is not string.但是,bedType 不是字符串。 Not String bedType but BedType bedType(Object).不是String bedType而是BedType bedType(Object)。 Here is my repository这是我的存储库

    @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 FindRoomStatus.Java

public class FindRoomStatus {

private BedType bedType;

public BedType getBedType() {
    return bedType;
}

public void setBedType(BedType bedType) {
    this.bedType = bedType;
}

In my controller, I have got error在我的控制器中,我有错误

String cannot be a converted to BedType字符串不能转换为 BedType

data = roomDetailRepository.findByBedType(findRoomStatus.getBedType().toString(), paging);

Here is BedType.Java这是 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;
}

List Room Status, in this list, i want to find by BedType列出房间状态,在此列表中,我想按床型查找

 {
            "id": 105,
            "roomId": 43,
            "floor": "1",
            "roomNumber": "001",
            "description": "Normal",
            "status": "Vacant Clean"
        },
        {
            "id": 11,
            "bedTypeName": "King size"
        },
        {
            "id": 39,
            "categoryName": "President Suite"
        }

You are using LIKE keyword in the query but you are accepting BedType object as a parameter in your query.您在查询中使用LIKE关键字,但您接受BedType对象作为查询中的参数。 You are sending String as an argument from your controller.您正在从控制器发送String作为参数。 This is the problem.这就是问题。 toString method will give the string representation of an object. toString方法将给出一个对象的字符串表示。

What you can do is, change the parameter to String which accepts bedTypeName like:您可以做的是,将参数更改为接受bedTypeName String ,例如:

@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);

And from the controller,从控制器,

data = roomDetailRepository.findByBedType(findRoomStatus.getBedType().getBedTypeName(), 
paging);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM