using projections on multiple columns

Good day.

So i have mapping @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;

how do i use projections to extract only

discountFare id discount fareCode and aircompany_id (the key of AircompanyRB)

So basically all the fields of DiscountFares plus only key of AircompanyRB?

I tried

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

but it throws exception saying AircompanyRB doesnt have a field "discount" (yes, it does not- DiscountFares have)

thank you very much! any help is highly appreciated


First of all, you're making your life difficult by using a Criteria query instead of a dead simple HQL query:

select df.id, df.discount, df.fareCode, ac.id 
from DiscountFares df
left join df.aircompanyId ac

Now, if you really want to use a Criteria, just translate the above query:

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/90894.html

上一篇: 休眠ID序列结果为空

下一篇: 在多列上使用投影