显示获取结果jsp和Hibernate

时间:2017-10-29 13:35:07

标签: hibernate

Hie Everyone。

首先,我要特别感谢@Biznet在我之前的帖子中指出违反Java命名约定的行为,并指导我完成之前的尝试。非常感谢!!!。

我一直在研究hibernate参数查询。结果是根据搜索框中传递的字符串显示提取的结果。搜索D的示例结果将显示Dan,Dennis,Dorothy。与A,F或S相同。

当前的障碍我的jsp页面没有显示任何内容并且没有错误在服务器端Wild-fly。页面正在加载并且没有显示任何内容。我试图将A %%参数交换为A%,仍然没有显示任何内容。这是我的代码:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="author_id")
private int id;

@Column(name="author_name")
private String author_Name;  
  getters and setters 


DAOImpl

@Override
@Transactional
public List<Author> getAuthors(){
Session currentSession =    sessionFactory.getCurrentSession();


String searchString="A%%";
Query<Author> theQuery = 
currentSession.createQuery("select a"+
"from Author a"+  
"where a.author_Name like searchString",Author.class);
 theQuery.setParameter("searchString",searchString+"%");  
List<Author> authors = theQuery.getResultList();
    return authors;


@Controller
@RequestMapping("/processForm")
public String listAuthors(Model theModel) {
List<Author> theAuthors = authorDAO.getAuthors();       
    theModel.addAttribute("authors", theAuthors);
    return "list-authors";



list-authors.jsp
           <th>Author Name</th>
        <c:forEach var="tempAuthor" items="${author}">              <td>${tempAuthor.author_Name}

2 个答案:

答案 0 :(得分:0)

我在详尽研究http://www.codejava.net知识库后解决了这个问题。

@Transactional
 @SuppressWarnings("unchecked")   
 //Passed parameter name in my method to be called by controller class    
  public List<Author> getAuthors(String name) {
  Session tsession = sessionFactory.getCurrentSession();                 
  String hql = "from Author where author_name like :keyword";
  Query query = tsession.createQuery(hql);
  query.setParameter("keyword", "%" + name + "%");            
  List<Author>authors =query.getResultList();          
   return authors;



@RequestMapping("/processForm")
//The controller will forward method call to DAO class 
public String getAuthors(@RequestParam(name = "text") String keyword, Model thModel) {  
   //The  DAO method forward the request using keyword      
      List<Author>authors=authorDao.getAuthors(keyword);    
      thModel.addAttribute("author", authors);      
//The selected column is displayed in jsp based on keyed string.      
 return "results";

}

答案 1 :(得分:0)

在你的HSQL案例中,你在参数名:之前缺少searchString:name是在HSQL中定义命名查询参数的方法