为了账号安全,请及时绑定邮箱和手机立即绑定

休眠从子表中选择查询

休眠从子表中选择查询

慕无忌1623718 2021-04-02 09:40:45
我很冬眠。我有两个表具有一对多的关系。两张表是: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。那么,我该如何在休眠状态下实现呢?
查看完整描述

1 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

当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(); 


查看完整回答
反对 回复 2021-04-28
  • 1 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信