Home > Enterprise >  How to find a list of an object by a nested table?
How to find a list of an object by a nested table?

Time:01-26

Such a problem. I have an entity. I need to get a list of objects of this entity by selection, namely by the body_material field. That is, for example, find all BoatCards whose body_material index is 2. I do this in Java using Spring. I have added Dao, Service, Controller and entity's classes

BoatBodyMaterial entity:

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

BoatCards entity:

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

Dao class:

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

Service class:

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

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

Controller class:

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

        return boatCardsService.getAllByMaterial();
    }

I get ERROR:

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

I resolve this problem! Thank you!

public interface BoatCardsDao extends JpaRepository<BoatCards, Integer> {

    List<BoatCards> findAllByBodyMaterialMatcode(Integer bodyMaterial);


}

CodePudding user response:

Try something like this:

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);
}
  • Related