0

I have a DAO java class. In the DAO, I execute a native SQL Query which outputs two records from the database. The results is printed on the console. However, when the method is called, only one record is returned. Why?

DAO Java Class:

public Showroom SearchShowroom(String carMake){ 

    Session session = sessionFactory.openSession(); 
    Transaction tx = session.beginTransaction();
    SQLQuery query =  session.createSQLQuery("SELECT * from showroom);

        tx.commit();
        session.close(); 
    return sw;  
}
Baadshah
  • 63
  • 6
  • Comment 1: Why do you commit a readonly transaction? – borjab Feb 01 '16 at 11:55
  • Comment 2: Do not use string concatenation for SQL Queries use parameters. You are opening your code to SQL injections. – borjab Feb 01 '16 at 11:56
  • Comment 3: Are you using Spring? If this is the case you might be better using the Spring @Transactional annotation in a service. This way you have the flexibility to combina calls to DAOs in a big transaction when you need. And you can separate the transactional scope from the operation – borjab Feb 01 '16 at 11:58
  • I am not using Spring. Its for learning purposes ! – Baadshah Feb 01 '16 at 12:04
  • Then, Baadshah, you may prefer to use it this way. There is a Transactional annotation not related to Spring. But this is just throw away code so you are OK https://javaee-spec.java.net/nonav/javadocs/javax/transaction/Transactional.html – borjab Feb 01 '16 at 12:16
  • @borjab: Recommending Spring to anyone who barely grasps basic Java goes a bit overboard. I'd rather recommend him putting his current "Java EE" project aside and practice more with basic Java by working through Oracle's own tutorials and an OCP book. All his questions posted so far are only based on a fundamental misunderstanding of the Java language itself, not of the frameworks/libraries used. – BalusC Feb 01 '16 at 14:10
  • Surely Spring might be another very complex thing. Just needed to check if he was using it as the DAO is used in the MVC patter that typically uses Spring – borjab Feb 01 '16 at 15:11

1 Answers1

3

You are only returning the last Showroom object which you create in your loop (and discard all others). If you want to return all of them, add them to a List and return that List as the result:

public List<Showroom> SearchShowroom(String carMake){ 
    ...
    List<Showroom> allResult = new ArrayList<>();
    for(Object[] data : result){
        Showroom sw = new Showroom();
        ...
        allResult.add(sw);
    }
    ...

    return allResult;  
}

Besides that immediate fix to your question, please also consider the comments from @borjab. Especially, never use string concatenation to inject variables into SQL statements - always use bind variables. See What is SQL injection? for more information.

Community
  • 1
  • 1
Andreas Fester
  • 34,015
  • 7
  • 86
  • 113
  • I have modified my Managed Bean as follows: `public List find() throws Exception{ ShowroomManager sm = new ShowroomManager(); return sm.SearchShowroom(make1); }` – Baadshah Feb 01 '16 at 12:01
  • My JSF is as follows: http://pastebin.com/0EcT2duN But it still does not display ! :( Help please – Baadshah Feb 01 '16 at 12:02
  • Probably this will help: http://stackoverflow.com/questions/9186364/how-do-i-display-a-list-of-items-from-a-bean-onto-a-jsf-webpage – Andreas Fester Feb 01 '16 at 12:05
  • Unfortunately this is quite different from what I am trying to achieve ! Please help bro. – Baadshah Feb 01 '16 at 12:07
  • Ester This is only for testing purposes. Please advise on how to retrieve the 2 records from the ManagedBean. – Baadshah Feb 01 '16 at 12:30
  • @Baadshah Probably its best to ask a new question with the details of your jsf page - ideally provide a [mcve]. – Andreas Fester Feb 01 '16 at 13:34
  • 1
    Just learn to ask one focused Question per question. A question consisting of 100 lines of code surely isn't a focused question. Cutting it down to the real problem yields better answers faster. Andreas already answered the current question. If you have a new question which does in no way relate to the current problem of having only one record returned from the method call instead of a list of records, do not post it as a comment but as a new Question. If you're not satisfied with this way of asking questions, look for an old fashioned discussion forum instead of a Question&Answer site. – BalusC Feb 01 '16 at 18:38
  • balusC bous ou liki dow moV pilon falourmama.. :) – Baadshah Feb 01 '16 at 19:36