WowBow
WowBow

Reputation: 7613

Criteria list returning empty list but there are rows in oracle db

I run sql statement "SELECT * FROM company" on oracle Thin 11 g and it returns 3 rows.

I have configured the data source correctly and tried Query as well Criteria as follows and all of them return nothing even if they should return 3 rows. //import org.hibernate.Criteria;

1. Criteria

 Criteria criteria = session.createCriteria(Company.class);
            criteria
                     .add(Restrictions.eq("companyName", companyName))
                     .add(Restrictions.eq("companyId", companyId));

List<Company> companyList  = criteria.list();//**THIS RETURNS 0 ROWS**

2. QUERY

Query query=session.createQuery("from Company where companyName= :companyName and companyId= :companyId");

           query.
                   setParameter("companyId",companyId).
                   setParameter("companyName", companyName);

List<Company> companyList  = query.list();//**THIS RETURNS 0 ROWS**

3.SQL in Query

Query query=session.createSQLQuery("SELECT * FROM Company");
List<Company> CompanyList  = query.list();//**THIS RETURNS 0 ROWS**

Here Company Entity

//javax.persistence.*;

@Entity
@Table(name = "COMPANY")
public class Company {

    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="COMPANY_SEQ")
    @SequenceGenerator(name="COMPANY_SEQ", sequenceName="COMPANY_SEQ",   allocationSize=1)
    @Column(name = "COMPANY_ID")
    int CompanyId;

    @Basic
    @Column (name ="COMPANY_NAME")
    private String companyName;


    @Basic
    @Column (name ="COMPANY_ID")
    private Integer companyId;// The login id of user whose data is updated

    @Basic
    @Column (name ="UPDATED_BY")
    private String updatedBy;

    @Basic
    @Column (name ="UPDATE_DATE")
    private Date updateDate;

     //Default no-arg constructor and another constructor with all the fields.
    //default getters and Setters
}

What am I missing and why is not all the hibernate queries not returning the rows? I appreciate your help.

Upvotes: 0

Views: 3680

Answers (2)

Messias Lima
Messias Lima

Reputation: 181

I got the same problem. I just forgot to add mapping-class in the hibernate.cfg.xml. I did it and works greatly

Upvotes: 1

WowBow
WowBow

Reputation: 7613

Thanks for @JBNizet's comment on my question. The problem was I wasn't committing the data in SQL Developer.

Make sure to 'commit' the transaction on the DB Developer you are using.

Here is the exact comment from @JBNizet

A transaction will see its own changes, even if the transaction is not committed yet. Other concurrent transactions won't see the uncommitted changes of the first transaction. That's the principle of transaction isolation (when it's set to READ_COMMITTED at least). Execute commit; in SQL developer to be sure. –

Upvotes: 3

Related Questions