美好的一天。
所以我有映射 @ID @GeneratedValue(strategy = GenerationType.SEQUENCE,generator =“discount_fares_id_sequence”) @SequenceGenerator( name =“discount_fares_id_sequence”, sequenceName =“discount_fares_id_seq”, allocationSize = 1 ) private Long id;
@Column
private Long discount;
@Column(name = "fare_code", length = 255)
private String fareCode;
@ManyToOne
@JoinColumn(name = "aircompany_id", foreignKey = @ForeignKey(name = "fk_discfares_ref_aircompany_id"))
private AircompanyRB aircompanyId;
如何使用投影仅提取
discountFare id 折扣 fareCode 和aircompany_id(AircompanyRB的关键)
所以基本上所有的DiscountFares字段加上只有AircompanyRB的密钥?
我试过
DetachedCriteria criteria = DetachedCriteria.forClass(DiscountFares.class)
.createCriteria("aircompanyId")
.setProjection(Projections.projectionList()
.add(Projections.property("id"))
.add(Projections.property("discount"))
.add(Projections.property("fareCode")));
List<DiscountFares> result = criteria.getExecutableCriteria(sessionFactory.getCurrentSession()).list();
但它抛出异常,说AircompanyRB没有字段“折扣” (是的,它没有 - DiscountFares有)
非常感谢你! 任何帮助都非常感谢答案 0 :(得分:1)
首先,您通过使用Criteria查询而不是简单的简单HQL查询来改变您的生活:
select df.id, df.discount, df.fareCode, ac.id
from DiscountFares df
left join df.aircompanyId ac
现在,如果你真的想使用Criteria,只需翻译上面的查询:
Criteria c = session.createCriteria(DiscountFares.class, "df");
c.createAlias("df.aircompanyId", "ac", JoinType.LEFT_OUTER_JOIN);
c.setProjection(Projections.projectionList()
.add(Projections.property("df.id"))
.add(Projections.property("df.discount"))
.add(Projections.property("df.fareCode"))
.add(Projections.property("ac.id")));