浏览 4621 次
锁定老帖子 主题:关于关联的两个表之间的批量添加数据错误
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2003-10-16
package com.getdown.hibernate; public class Post { private Long id; private String title; private String content; private User owner; public Post(); { } public Post(String title); { this.title = title; } public Long getId(); { return id; } private void setId(Long long1); { id = long1; } public String getTitle(); { return title; } public void setTitle(String string); { title = string; } public String getContent(); { return content; } public void setContent(String string); { content = string; } public User getOwner(); { return owner; } public void setOwner(User user); { owner = user; } public boolean equals(Object obj); { if(!(obj instanceof Post);); return false; return ((Post);obj);.getId();.equals(id);; } } package com.getdown.hibernate; import java.util.HashSet; import java.util.Set; public class User { private Long id; private String mail; private String password; private String nick; private Set posts; private User(); {} public User(Long id); { id = id; posts = new HashSet();; } public User(String nick); { this.nick = nick; posts = new HashSet();; } public Long getId(); { return id; } private void setId(Long id); { id = id; } public String getMail(); { return mail; } public void setMail(String string); { mail = string; } public String getPassword(); { return password; } public void setPassword(String string); { password = string; } public String getNick(); { return nick; } public void setNick(String string); { nick = string; } public Set getPosts(); { return posts; } public void setPosts(Set set); { posts = set; } public boolean equals(Object obj); { if(!(obj instanceof User);); return false; return ((User);obj);.getId();.equals(id);; } } 两个配置文件如下: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.getdown.hibernate.Post" table="POST" dynamic-update="false" dynamic-insert="false" > <id name="id" column="POST_ID" type="java.lang.Long" > <generator class="identity"> </generator> </id> <property name="title" type="java.lang.String" update="true" insert="true" column="TITLE" /> <property name="content" type="java.lang.String" update="true" insert="true" column="CONTENT" /> <many-to-one name="owner" class="com.getdown.hibernate.User" cascade="all" outer-join="auto" update="true" insert="true" column="OWNER_ID" /> </class> </hibernate-mapping> <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.getdown.hibernate.User" table="USER" dynamic-update="false" dynamic-insert="false" > <id name="id" column="USER_ID" type="java.lang.Long" > <generator class="identity"> </generator> </id> <property name="mail" type="java.lang.String" update="true" insert="true" column="E_MAIL" /> <property name="password" type="java.lang.String" update="true" insert="true" column="PASSWORD" /> <property name="nick" type="java.lang.String" update="true" insert="true" column="NICK_NAME" /> <set name="posts" lazy="true" cascade="save-update" inverse="true" sort="unsorted" order-by="POST_ID" > <key column="OWNER_ID" /> <one-to-many class="com.getdown.hibernate.Post" /> </set> </class> </hibernate-mapping> 测试类: public class UserHibernate { public static void main(String[] args); throws Exception { Configuration cfg = new Configuration();.addClass(User.class);.addClass(Post.class);; SessionFactory sessions = cfg.buildSessionFactory();; Session session = sessions.openSession();; Set set = new HashSet();; User user = (User); session.find("from User user where user.id='1'");.get(0);; for (int i = 0; i < 10; i++); { Post post = new Post("a");; post.setOwner(user);; // session.save(post);; set.add(post);; } user.setPosts(set);; session.save(user);; session.flush();; session.close();; } } 这样两个表数据都可以数据都能添加 但是两个表之间的关联字段 即post里面的 OWNER_ID 为 null 抛出异常 net.sf.hibernate.HibernateException: identifier of an instance of com.getdown.hibernate.User altered from 1 to null 哪位大虾帮一下 呵呵 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2003-10-16
引用 session.save(user); 这句是不需要的,Session.save()其实就是insert,对于在同一个Session中载入并作了修改而需要保存的对象只需要Session.flush()就行了。 加上事务处理。 引用 <many-to-one name="owner" class="com.getdown.hibernate.User" cascade="all" outer-join="auto" update="true" insert="true" column="OWNER_ID" /> many-to-one的cascade设为"all"是不对的,改为"save-update"好了。 |
|
返回顶楼 | |
发表时间:2003-10-16
这样改过改完了 还是一样的错误...
|
|
返回顶楼 | |
发表时间:2003-10-16
引用 private void setId(Long id) { id = id; } 你的程序中还有好几个类似的错误!!!!!! |
|
返回顶楼 | |
发表时间:2003-10-16
极端不好意思 我以后一定仔细看完再发
浪费了yeh220那么多时间 抱歉 |
|
返回顶楼 | |