Hibernate and mssql inner join rowcount

Posted by ez2sarang on Stack Overflow See other posts from Stack Overflow or by ez2sarang
Published on 2012-11-29T04:37:36Z Indexed on 2012/11/29 5:04 UTC
Read the original article Hit count: 118

Filed under:
|

I am struggling with Hibernate Criteria. My aim is to create the following request using 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=?

Here is my code:

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

However, the results ...

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

Where Inner join?

Thank you for help!

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate