猿问

休眠从子表中选择查询

我很冬眠。我有两个表具有一对多的关系。两张表是:


public class Pashmina implements Serializable {

    @Id

    @GeneratedValue(strategy = GenerationType.AUTO, generator = "sq_pashmina_id")

    @SequenceGenerator(name = "sq_pashmina_id", sequenceName = "sq_pashmina_id")

    @Column(name = "PASHMINA_ID")

    private int pashminaId;


    @Column(name = "PASHMINA_NAME")

    private String pashminaName;


    @Column(name = "PRICE")

    private double price;


    @Column(name = "ADDED_AT", insertable = false)

    @Temporal(TemporalType.TIMESTAMP)

    private Date addedAt;


    @Column(name = "CATEGORY")

    private String category;


    @Column(name = "ENABLED", insertable = false)

    private Character enabled;


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

    private Set<PashminaColour> pashminaColor  = new HashSet<PashminaColour>();


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

    private Set<Image> images  = new HashSet<Image>();


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

    private Set<Description> descriptions  = new HashSet<Description>();


    //getter and setter method

这是一个父类,它与Image表具有一对多关系


public class Image implements Serializable {

    @Id

    @Column(name = "IMAGE_ID")

    private int imageId;


    @Column(name = "IMAGE_NAME")

    private String imageName;


    @JoinColumn(name = "PASHMINA_ID", referencedColumnName = "PASHMINA_ID")

    @ManyToOne

    private Pashmina pashmina;

现在,我要使用其父类的ID(即pashminaId)imagenames从Image类中选择一个


例如:


从TBL_IMAGE中选择IMAGE_NAME,其中PASHMINA_ID ='some_digit';


我如何在图像类中传递pashminaId,因为没有pashminaId它只有Parent类的Object创建Pashmina。


那么,我该如何在休眠状态下实现呢?


慕无忌1623718
浏览 147回答 1
1回答

哈士奇WWW

当Hibernate对待对象而不是SQL表时,您可以简单地pashmina从您的image对象访问该对象,并且在查询中您将对待Java对象/ POJO,因此可以通过对其进行访问Image.pashmina.pashminaId。因此,您可以编写以下查询:String hql = "select I.imageName FROM Image I WHERE I.pashmina.pashminaId = 10";Query query = session.createQuery(hql);List results = query.list();&nbsp;
随时随地看视频慕课网APP

相关分类

Java
我要回答