在hibernate中解决java.lang.StackOverflowError

来源:互联网 发布:淘宝卖家使用软件 编辑:程序博客网 时间:2024/05/20 20:19

好长时间没用过hibernate,今天突然用到,发现一个小小的错误产生,不知何种原因,知道在CSDN上看到末尾仁兄的博客才知道解决办法,先分享。

@WebServlet("/GoodsServlet")
public class GoodsServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private static GoodsDao dao = new GoodsDaoImpl();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String action = request.getParameter("action");
if (action.equals("selectGoodsList")) {
try {
List<GoodsBean> list = dao.selectGoodsList();
request.setAttribute("goods", list);
request.getRequestDispatcher("goods/goodsList.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}


public class GoodsDaoImpl implements GoodsDao {


private Connection conn;
private PreparedStatement preStatement;
private ResultSet res;


/**
* 查询商品列表
* @throws SQLException 
*/
@Override
public List<GoodsBean> selectGoodsList() throws SQLException {
// TODO Auto-generated method stub
DBConnection db = new DBConnection();
conn = db.getConn();
String sql = "select g.gid,g.gname,g.price,g.detail,g.tid,t.tname from tbl_goods g,tbl_type t where g.tid=t.tid";
preStatement = conn.prepareStatement(sql);
res = preStatement.executeQuery();
List<GoodsBean> list = new ArrayList<GoodsBean>();
while(res.next()){
GoodsBean goods = new GoodsBean();
TypeBean type = new TypeBean();
goods.setGid(res.getInt("gid"));
goods.setGname(res.getString("gname"));
goods.setPrice(res.getBigDecimal("price"));
goods.setDetail(res.getString("detail"));
goods.setTid(res.getInt("tid"));
type.setTid(res.getInt("tid"));
type.setTname(res.getString("tname"));

goods.setType(type);
list.add(goods);
}
conn.close();
return list;
}

}


此运行的时候一直报错java.lang.StackOverflowError

之前是把this.doPost(),写为this.doGet()造成死循环。

解决

原创粉丝点击