-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate keyword search.. (need help)
PostPosted: Fri Oct 10, 2008 3:42 am 
Newbie

Joined: Fri Oct 10, 2008 3:04 am
Posts: 1
Hi all,

I am very very new to hibernate and I am currently trying to do a search function whereby a word keyed in by the user will return all attributes containing that word in a class. But i keep encountering problems in it. The problem is highlighted at end of the codes.

my codes for search.jsp is as follows:
.........
<div id="content" style="height:400px">

<s:form action="Search" method="POST">
<s:textfield name="searchName" label="Enter facility name"/>
<s:submit value="Search"/>
</s:form>
<s:actionerror/>
<s:fielderror/>

</div> <!-- content -->
.........

where my SearchAction.java as follows:

public class SearchAction extends ActionSupport {

private Facility facility;
private SearchDAO searchDAO = new SearchDAO();
private String name = null;
private List<Facility> searchFacilityList = new ArrayList<Facility>();
private List<Facility> facilitiesList;
private FacilityDAO facilityDAO = new FacilityDAO();
private String searchName = null;

public String execute() throws Exception {
searchFacilityList = searchDAO.searchByName(searchName);

if(searchFacilityList != null){
Map session = ActionContext.getContext().getSession();
session.put("searchFacilityList", searchFacilityList);
return "success";
}
else {
addActionError("Invalid facility name. Please try again.");
return ERROR;
}
}

public List<Facility> getSearchFacilityList() {
return searchFacilityList;
}

public void setSearchFacilityList(List<Facility> searchFacilityList) {
this.searchFacilityList = searchFacilityList;
}

public String getSearchName() {
return searchName;
}

public void setSearchName(String searchName) {
this.searchName = searchName;
}
}

and finally my SearchDAO.java:

public class SearchDAO extends GenericDAO {

private Facility facility;
private FacilityDAO facilityDAO = new FacilityDAO();
private List<Facility> facilityList;
private Criteria crit;

public SearchDAO() {
super(Facility.class);
}

public List<Facility> searchByName(String searchName) {
//facilityList = facilityDAO.getAllFacilities();

try {
SessionFactory factory = HibernateUtil.getSessionFactory();
this.session = factory.getCurrentSession();
session.beginTransaction();

//Criteria Query Example
crit = session.createCriteria(Facility.class);

crit.add(Restrictions.like("name", "%searchName%"));
crit.add(Restrictions.like("description", "%searchName%"));
crit.add(Restrictions.like("location", "%searchName%"));
crit.add(Restrictions.like("type", "%searchName%"));

List<Facility> results = crit.list();
return results;
} finally {
session.close();
}
}
}

I am trying to load all the results onto a new page named searchresult.jsp. but whenever I run this portion, it just goes into searchresult.jsp with no results shown. Even when I keyed in nothing, it will go to the searchresult.jsp instead of showing the error message.

Can anyone please guide me?


Jeremy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2008 2:56 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi Jeremy,

it is hard to say what goes wrong in your code. Overall it looks ok, but without the configuration files and log output it is hard to say what's actually happening. It seems you are using Struts2 and Hibernate. You find an example setup (including Hibernate Search, but you can just ignore that part) on the Hibernate wiki: http://www.hibernate.org/441.html. I also recommend you to read about the Open Session in View pattern (http://www.hibernate.org/43.html). In your setup you would get problems problems when using lazy loading since the session will be closed at the time you want to render the result (not important of course in case you don't use lazy loading).

Last but not least I recommend you do check out some of the excellent struts tutorials on the struts homepage. For example you don't have to explicitly set the result list into the session. Struts 2 has very powerful dependency injection capabilities.

Good luck :)

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.