在SpringBoot中将本机查询映射到非实体类

时间:2020-05-17 19:22:35

标签: java spring hibernate spring-boot jpa

我还是Jpa和Springboot的新手,所以我正在开发一个应用程序,该应用程序要从2表中检索特定列,以下是SearchResponse类的代码段。假设将查询结果映射到SearchResponse

df3 = df1.merge(df2,on='Accident_ID')
df3["train"] = df3.Accident_ID < 5 
df3["train"] .value_counts()

triples = []
for _, row in df3[df3["train"]].iterrows():

    if row["ROLE"] == "Driver":
        if row["User_ID"] == row["DriverID_1"]:
            Drives = (row["User_ID"],row["CarID_1"], "Drives")
        elif row["User_ID"] == row["DriverID_2"]:  
            Drives = (row["User_ID"],row["CarID_2"], "Drives")
    else:    
        Witness = (row["User_ID"],row["Accident_ID"], "Witness") 

    Involved_in_first = (row["CarID_1"],row["Accident_ID"], "Involved in")
    Involved_in_second = (row["CarID_2"],row["Accident_ID"], "Involved in")
    Happened_in = (row["Accident_ID"],row["Location"], "Happened in")
    Lives_in = (row["User_ID"],row["Address"], "Lives in")
    triples.extend((Drives , Witness  , Involved_in_first,Involved_in_second, Happened_in , Lives_in ))


triples_df = pd.DataFrame(triples, columns=["Source", "Target", "Edge"])
triples_df.shape

RoomRepository,

package com.nayiroom.model.customs;



import javax.persistence.*;
import java.util.UUID;


@SqlResultSetMapping(
        name = "SearchResponseMap",
        classes = @ConstructorResult (
                        targetClass = SearchResponse.class,
                        columns = {
                                @ColumnResult(name="rent",type = Double.class),
                                @ColumnResult(name="deposit",type = Double.class),
                                @ColumnResult(name="city",type = String.class),
                                @ColumnResult(name="room_id",type = UUID.class)
                        }
                        )
)
@NamedNativeQuery(
        name = "SearchResponseQuery.SearchResults",
        query="select rent, deposit, city, suburb, room_id\n" +
                "from room\n" +
                "join address\n" +
                "on  address.address_id = room.address_address_id",
        resultSetMapping = "SearchResponseMap")

public class SearchResponse {

    private double rent;
    private  double deposit;
    private String city;
    private  UUID room_id;

    public SearchResponse(double rent, double deposit, String city, UUID room_id) {
        this.rent = rent;
        this.deposit = deposit;
        this.city = city;
        this.room_id = room_id;
    }

    public double getRent() {
        return rent;
    }

    public void setRent(double rent) {
        this.rent = rent;
    }

    public double getDeposit() {
        return deposit;
    }

    public void setDeposit(double deposit) {
        this.deposit = deposit;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public UUID getRoom_id() {
        return room_id;
    }

    public void setRoom_id(UUID room_id) {
        this.room_id = room_id;
    }
}

但是当我尝试运行应用程序时,出现此错误

@Repository
public interface RoomResponseRepository extends JpaRepository<SearchResponse,Long> {


    @Query(name = "SearchResponseQuery.SearchResults")
    List<SearchResponse> SearchResults();

}

1 个答案:

答案 0 :(得分:0)

您只能在实体类上使用@SqlResultSetMapping和@NamedNativeQuery。从JpaRepository扩展的RoomResponseRepository应该指向您的“ Room”实体,该实体可能映射为@Entity。您的SearchResponse类应该是DTO类。

@Repository
public interface RoomResponseRepository extends JpaRepository<Room,Long> {
    
    @Query(name = "SearchResponseQuery.SearchResults")
    List<SearchResponse> SearchResults();

}