0

Hibernate Criteria に苦労しています。私の目的は、 Hibernate Criteria を使用して次のリクエストを作成することです:

select
    count(*) as y0_ 
from
    PInterface this_ 
inner join
    Product product2_ 
        on this_.product_id=product2_.id 
where
    this_.product_interface_type_id=?

これが私のコードです:

@Entity @Table(name = "PInterface")
public class PInterface {
    @Id
    @GeneratedValue
    @Column(name = "id", nullable = false, insertable = false, updatable = false)
    private int id;
    @Column(name = "product_id")
    private int productId;
    @Column(name = "product_interface_type_id")
    private int type;

    @ManyToOne(optional=false)
    @JoinColumn(name = "product_id", referencedColumnName = "id", insertable=false, updatable=false)
    private Product product;
}
@Entity @Table(name = "Product")
public class Product {
    @Id
    @GeneratedValue
    @Column(name = "id", nullable = false, insertable = false, updatable = false)
    private int id;
    private String name;
}

//Criteria is :
Object criteria = sessionFactory.getCurrentSession()
                    .createCriteria(PInterface.class)
                    .add(Restrictions.eq("type", 1))
                    .setProjection(Projections.rowCount())
                    .uniqueResult()
                    ;

しかし、結果は...

select
    count(*) as y0_ 
from
    PInterface this_ 
where
    this_.product_interface_type_id=?

インナーはどこに参加しますか?

ご協力ありがとう御座います!

4

1 に答える 1

0

これはどう:

           Object criteria = sessionFactory.getCurrentSession()
                .createCriteria(PInterface.class)
                .createCriteria("product")
                .createAlias("product", "p")
                .add( Restrictions.eqProperty("p.id", "productId") )
                .add(Restrictions.eq("type", 1))
                .setProjection(Projections.rowCount())
                .uniqueResult();
于 2012-11-29T05:00:33.107 に答える