在多列上使用投影

美好的一天。

所以我已经映射@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折扣票价代码和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有)

非常感谢你! 任何帮助,高度赞赏


首先,你通过使用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")));
链接地址: http://www.djcxy.com/p/90893.html

上一篇: using projections on multiple columns

下一篇: JPA with SequenceGenerator and GeneratedValue