java 如何通过嵌套表查找对象的列表?

62lalag4  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(144)

这样的问题,我有一个实体,我需要通过选择得到这个实体的对象列表,也就是通过body_material字段,也就是比如说找到body_material索引为2的所有BoatCards,我是用Spring在Java中做的,我添加了Dao,Service,Controller和实体的类
车身材料实体:

@Entity
@Table(name = "boat_body_material")
@Data
@NoArgsConstructor
    public class BoatBodyMaterial {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "matcode")
        private Integer matcode;
    
        @Column(name = "matname")
        private String matname;
    
        @Column(name = "matnote")
        private String matnote;
    
    }

船卡实体:

@Entity
@Table(name = "boat_cards")
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
    public class BoatCards {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "cardid")
        Long cardid;
    
        @Column(name = "reg_num")
        String regNum;
    
        @Column(name = "tiket_num")
        String tiketNum;
    
        @Column(name = "boat_name")
        String boatName;
    
        @ManyToOne
        @JoinColumn(name = "boat_type")
        BoatTypes boatType;
    
        @Column(name = "boat_year")
        String boatYear;
    
        @Column(name = "boat_vin")
        String boatVin;
    
        @Column(name = "parking_place")
        String parkingPlace;
    
        @ManyToOne
        @JoinColumn(name = "sa_category")
        SaCategory saCategory;
    
        @Column(name = "boat_length")
        String boatLength;
    
        @Column(name = "boat_width")
        String boatWidth;
    
        @Column(name = "boat_height")
        String boatHeight;
    
        @ManyToOne
        @JoinColumn(name = "body_material")
        BoatBodyMaterial bodyMaterial;
    
        @Column(name = "boat_payload")
        Long boatPayload;
    
        @Column(name = "passengers_num")
        Long passengersNum;
    
        @Column(name = "service_life")
        String serviceLife;
    
        @Column(name = "engine_num")
        Long engineNum;
    
        @ManyToOne
        @JoinColumn(name = "owner")
        PersonData owner;
    
        @ManyToOne
        @JoinColumn(name = "agent")
        PersonData agent;
    
        @Column(name = "note")
        String note;
    
    }

刀类:

public interface BoatCardsDao extends JpaRepository<BoatCards, Integer> {

    @Query(value = "SELECT * from gims.boat_body_material where matcode = 1", nativeQuery = true)
    BoatBodyMaterial findByBodyMaterial ();

    List<BoatCards> findAllByBodyMaterial(BoatBodyMaterial list);
}

服务等级:

public List<BoatCards> getAllByMaterial() {
        BoatBodyMaterial matcodeFromTable = boatCardsDao.findByBodyMaterial();

        List<BoatCards> boatCards = boatCardsDao.findAllByBodyMaterial(matcodeFromTable);
        return boatCards;
    }

控制器类:

@GetMapping(path="/get")
    public List<BoatCards> get() {

        return boatCardsService.getAllByMaterial();
    }

我得到错误:

2023-01-26T10:33:20.193+03:00 ERROR 20852 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [by.compit.gimsshd.model.BoatBodyMaterial] for value '{1, metal, produce from metal}'] with root cause

我解决这个问题!谢谢!

public interface BoatCardsDao extends JpaRepository<BoatCards, Integer> {

    List<BoatCards> findAllByBodyMaterialMatcode(Integer bodyMaterial);

}
t3psigkw

t3psigkw1#

试试这样的方法:

public interface BoatCardsDao extends JpaRepository<BoatCards, Integer> {

    @Query(value = "SELECT card FROM BoatCards card"
            + " JOIN card.bodyMaterial mat"
            + " WHERE mat.matcode = :matcode")
    List<BoatCards> findAllByBodyMaterial(@Param("matcode") int matcode);
}

相关问题