当我使用Hibernate执行搜索时,我收到了
entities.Tbphonebook @(数字和字母组合)
在我的控制台日志中,而不是所有项目的列表。如果我将系统更改为打印到
system.out.println(person.getLastname)
我看到返回的对象中保留了姓氏。我想搜索名字,并返回姓名,姓氏,区号和电话号码。这可能是因为我使用折旧语言,但我很难弄清楚新版本应该是什么。我试图压制警告,但这并没有解决问题。
感谢您的帮助。
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add contact to Phone Book</title>
<h1>Add contact to Phone Book</h1>
</head>
<body>
<form name="submitInfo" method="get" action="AddEntryServlet">
First Name: <input type="text" name="firstName"/> <br/>
<br/>
Last Name: <input type="text" name="lastName"/> <br/>
<br/>
Area Code: <input type="number" name="areaCode"/> <br/>
<br/>
Phone Number: <input type="number" name="phoneNumber"/> <br/>
<br/>
<input type="submit" value="Submit" />
</form>
</body>
<h1>Search for Contact in Phone Book</h1>
<body>
<form name="searchInfo" method="get" action="SearchServlet">
Search First Name: <input type="text" name="searchFirstName"/> <br/>
<br/>
Search Last Name: <input type="text" name="searchLastName"/> <br/>
<br/>
<input type="submit" name="action" value="search" />
</form>
</body>
</html>
<br>
&#13;
SQL地图文件Tbphonebook
package entities;
// Generated Dec 13, 2017 10:03:41 PM by Hibernate Tools 5.2.6.Final
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Tbphonebook generated by hbm2java
*/
@Entity
@Table(name = "tbphonebook", schema = "public")
public class Tbphonebook implements java.io.Serializable {
private int id;
private String firstname;
private String lastname;
private Integer areacode;
private Integer phonenumber;
public Tbphonebook() {
}
public Tbphonebook(int id) {
this.id = id;
}
public Tbphonebook(int id, String firstname, String lastname, Integer areacode, Integer phonenumber) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.areacode = areacode;
this.phonenumber = phonenumber;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "firstname")
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Column(name = "lastname", length = 30)
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Column(name = "areacode")
public Integer getAreacode() {
return this.areacode;
}
public void setAreacode(Integer areacode) {
this.areacode = areacode;
}
@Column(name = "phonenumber")
public Integer getPhonenumber() {
return this.phonenumber;
}
public void setPhonenumber(Integer phonenumber) {
this.phonenumber = phonenumber;
}
}
搜索Servlet
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Query;
import org.hibernate.Session;
import entities.Tbphonebook;
import util.HibernateUtil;
/**
* Servlet implementation class SearchServlet
*/
@WebServlet("/SearchServlet")
public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SearchServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings({ "deprecation", "unchecked" })
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String firstName = request.getParameter("searchFirstName");
// String lastName = request.getParameter("searchLastName");
@SuppressWarnings("rawtypes")
Query query = session.createQuery("from Tbphonebook where firstname = ?");
query.setParameter(0, firstName);
List<Tbphonebook> persons = (List<Tbphonebook>) query.getResultList();
for(Tbphonebook person : persons)
System.out.println(person);
//response.getWriter().append("match" + person);
session.getTransaction().commit();
session.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}