0

I want to search in spring data jpa by bedType. But, bedType is not string. Not String bedType but 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

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

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

Here is 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"
        }

1 Answer 1

1

You are using LIKE keyword in the query but you are accepting BedType object as a parameter in your query. You are sending String as an argument from your controller. This is the problem. toString method will give the string representation of an object.

What you can do is, change the parameter to String which accepts bedTypeName like:

@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);
Sign up to request clarification or add additional context in comments.

5 Comments

In my repository change to String bedType? not BedType bedType?
In your controller, change findRoomStatus.getBedType().toString() to findRoomStatus.getBedType();
Then how to i find by bed type? in the postman like this { "bedType" : " " } String is " ", if not string what value of bedType : ?
Post your BedType class as well.
Please look again my post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.