Hibernate一对多(单向) - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-14
012-01-03 blogjava 天堂露珠
/index.jsp: <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ page import="wintys.hibernate.onetomany.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ''index.jsp'' starting page</title> </head> <body> <% List<User> users = null; HibernateDAO dao = new HibernateDAOBean(); dao.insert(); users = dao.selectAll(); Iterator<User> it = users.iterator(); while(it.hasNext()){ User user = it.next(); String id = user.getId(); String name = user.getName(); out.println("id:" + id + "<br />"); out.println("name:" + name + "<br />"); out.println("cards:<br />"); Set<Card> cards = user.getCards(); Iterator<Card> itc = cards.iterator(); while(itc.hasNext()){ Card card = itc.next(); String cardId = card.getId(); float balance = card.getBalance(); out.println(" cardId:" + cardId + "<br />"); out.println(" balance:" + balance + "<br />"); } out.println("<hr/>"); } %> </body> </html> 5、运行结果: 控制台显示: ...... Hibernate: insert into db.myuser (name, id) values (?, ?) Hibernate: insert into db.mycard (balance, id) values (?, ?) Hibernate: insert into db.mycard (balance, id) values (?, ?) Hibernate: insert into db.mycard (balance, id) values (?, ?) Hibernate: update db.mycard set userId=? where id=? Hibernate: update db.mycard set userId=? where id=? Hibernate: update db.mycard set userId=? where id=? ...... Hibernate一对多(单向)(9)时间:2012-01-03 blogjava 天堂露珠index.jsp页面显示: id:402881e421d4d0be0121d4d20e140005 name:Tom cards: cardId:402881e421d4d0be0121d4d20e230008 balance:654.8 cardId:402881e421d4d0be0121d4d20e230006 balance:7641.96 cardId:402881e421d4d0be0121d4d20e230007 balance:3650.0 6、注意的问题: a、错误提示:Field ''userId'' doesn''t have a default value。 一开始把"userId"设成NOT NULL,但是Hibernate先执行的是: "insert into db.mycard (balance, id) values (?, ?)" 然后才执行"update db.mycard set userId=? where id=?", 而userId在 |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于Hibernate一对多(单向) - 编程入门网的所有评论