我刚冬眠。我有两个具有一对多关系的表。两个表是:
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
,它只有父类Pashmina
的对象创建。
那么,我如何在休眠状态下实现这一目标?
谢谢!希望能得到积极的回应。
答案 0 :(得分:1)
当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();