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; 
}
asked Feb 1, 2016 at 11:32
7
  • Comment 1: Why do you commit a readonly transaction? Commented Feb 1, 2016 at 11:55
  • Comment 2: Do not use string concatenation for SQL Queries use parameters. You are opening your code to SQL injections. Commented Feb 1, 2016 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 Commented Feb 1, 2016 at 11:58
  • I am not using Spring. Its for learning purposes ! Commented Feb 1, 2016 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 javaee-spec.java.net/nonav/javadocs/javax/transaction/… Commented Feb 1, 2016 at 12:16

1 Answer 1

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.

answered Feb 1, 2016 at 11:50
Sign up to request clarification or add additional context in comments.

8 Comments

I have modified my Managed Bean as follows: public List<Showroom> find() throws Exception{ ShowroomManager sm = new ShowroomManager(); return sm.SearchShowroom(make1); }
My JSF is as follows: pastebin.com/0EcT2duN But it still does not display ! :( Help please
Probably this will help: stackoverflow.com/questions/9186364/…
Unfortunately this is quite different from what I am trying to achieve ! Please help bro.
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.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.