Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java程序分享区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
作者 第一次试验hibernate的one-to-many,不成功
guorui_wh





发贴: 12
于 2004-11-15 11:20 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我的环境是spring+hibernate
例子是Forum和Topic的one to many关系,是这样配置的
Forum:
<set name="topics" lazy="false" outer-join="auto" inverse="true"
  cascade="all" sort="unsorted" table="t_bbs_topic">
  <key column="forum_id"></key>
  <one-to-many class="com.bbs.po.hibernatepo.TbbsTopic" />
</set>

Topic:
<many-to-one name="forum"
  class="com.bbs.po.hibernatepo.TbbsForum" column="forum_id"
  outer-join="auto" cascade="none" access="property" update="true"
  insert="true" not-null="true">
</many-to-one>

Test code :


TbbsForum forum = findById(1);
System.out.println("Name:" + forum.getName());
TbbsTopic topic = new TbbsTopic();
topic.setContent("test");
topic.setCreateDate(Function.getSystime());
topic.setCreateUser("guorui_wh");
topic.setId(new Integer(234));
topic.setLastReplyDate(Function.getSystime());
topic.setLastReplyUser("guorui_wh");
topic.setReplyCount(0);
topic.setShowFlag(true);
topic.setTitle("asdfasdf");
topic.setTopFlag(true);

topic.setForum(forum);
forum.getTopics().add(topic);
getHibernateTemplate().save(forum);



作者 Re:第一次试验hibernate的one-to-many,不成功 [Re:guorui_wh]
guorui_wh





发贴: 12
于 2004-11-15 11:22 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Exception info:

Hibernate: insert into t_bbs_forum (name, master, create_date, create_user, remark, id) values (?, ?, ?, ?, ?, ?)

java.sql.BatchUpdateException: Invalid argument value: Duplicate entry '1' for key 1

为什么会是插入到forum 中去呢,我是按照夏昕的hibernate开发指南上一步一步作的



作者 Re:第一次试验hibernate的one-to-many,不成功 [Re:guorui_wh]
yipsilon





发贴: 248
于 2004-12-05 20:45 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Topic的key是不是原始类型的? 像int或long这样的?

如果是, 使用Integer或Long替代....



作者 Re:第一次试验hibernate的one-to-many,不成功 [Re:guorui_wh]
wenyuqiu





发贴: 14
于 2004-12-05 23:14 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有某个主键重复了吧自己再仔细查查看,



reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923