猿问

使用集合加载 DTO

@Entity

@Table(name = "person")

public class Consignment implements Serializable {


    @Id

    @GeneratedValue(strategy = GenerationType.AUTO)

    private Long id;


    @Column(name = "person_id")

    private String personId;


    @Column(name = "person_name")

    private String personName;


    @OneToMany(fetch = FetchType.EAGER, mappedBy = "person")

    @Column(name = "cars_owned")

    private Set<Cars> casrsowned = new HashSet<>();

}

@Entity

@Table(name = "cars")

public class Cars implements Serializable {


    @Id

    @GeneratedValue(strategy = GenerationType.AUTO)

    private Long id;


   @Column(name = "cars_id")

    private String carsId; 



  @ManyToOne

    @JoinColumn(name = "person")

    private Person person;


    @OneToOne

    private CarsDetail  carsDetail;

}

@Entity

@Table(name = "carsDetail")

public class CarsDetail implements Serializable {

    @Id

    @GeneratedValue(strategy = GenerationType.AUTO)

    private Long id;


    @Column(name = "brand")

    private String brand;


    @Column(name = "color")

    private String color;


    @Column(name = "model")

    private String model;

}

class CarModelDTO {


    String personName;

    List<String> models;

}

在上面的关系中,想要返回CarModelDTO JPA 查询 where,


@Query("Select CarModelDTO(p.personName, p.casrsowned.carsDetail.model) from Person as p where p`enter code here`.id = :id"))

public CarModelDTO getCarmodelOwnedByAperson(@Param("id") Long id);

我尝试了多种方法,但它给出了 org.hibernate.QueryException: illegal attempt to dereference collection


慕码人2483693
浏览 220回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答