0

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.

5
  • 1
    Probably because you're connecting to a different database/schema, or because the data has been inserted, but not committed yet. Commented May 26, 2015 at 20:04
  • @JBNizet I already run SELECT * FROM company and it retuned the rows on SQL Developer. So I know the data is committed. Commented May 26, 2015 at 20:28
  • 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. Commented May 26, 2015 at 20:31
  • 1
    @JBNizet I am just taking my hat off to say Thank you!! Commented May 26, 2015 at 20:50
  • I just added your response as an answer incase if other's encounter the same problem. Feel free to edit it! Commented May 26, 2015 at 21:03

2 Answers 2

3

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. –

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.