浏览 2993 次
锁定老帖子 主题:提问:unsaved的问题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2004-01-15
public class team { public team(); { } private String name; private Long team_id; private Set players = new HashSet();; public String getName(); { return name; } public void setName(String name); { this.name = name; } public Set getPlayers(); { return players; } public void setPlayers(Set players); { this.players = players; } public Long getTeam_id(); { return team_id; } public void setTeam_id(Long team_id); { this.team_id = team_id; } public boolean equals(Object o); { if (this == o); return true; if (this.team_id == null); return false; if (! (o instanceof Player);); return false; team team = (team); o; return this.team_id.equals(team.team_id);; } public int hashCode(); { return this.team_id == null ? super.hashCode(); : this.team_id.hashCode();; } } Player类 public class Player { public Player(); { } private Long id; private team belongTeam; private String name; public boolean equals(Object o); { if (this == o); return true; if (this.id == null); return false; if (! (o instanceof Player);); return false; Player c = (Player); o; return this.id.equals(c.id);; } public int hashCode(); { return this.id == null ? super.hashCode(); : this.id.hashCode();; } public Long getId(); { return id; } public String getName(); { return name; } public void setId(Long id); { this.id = id; } public void setName(String name); { this.name = name; } public team getBelongTeam(); { return belongTeam; } public void setBelongTeam(team belongTeam); { this.belongTeam = belongTeam; } } Team.hbm.xml <?xml version="1.0"?> <!-- edited with XMLSPY v5 rel. 2 U (http://www.xmlspy.com); by sunliqun (ces); --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="team" table="TEAM"> <id name="team_id" column="TEAM_ID" type="java.lang.Long" unsaved-value="null"> <generator class="assigned"> </generator> </id> <property name="name" column="NAME" type="java.lang.String"/> <set name="players" lazy="true" cascade="all-delete-orphan" inverse="true" sort="unsorted"> <key column="TEAM_ID"/> <one-to-many class="Player"/> </set> </class> </hibernate-mapping> Player.hbm.xml <?xml version="1.0"?> <!-- edited with XMLSPY v5 rel. 2 U (http://www.xmlspy.com); by sunliqun (ces); --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Player" table="PLAYER"> <id name="id" column="PLAYER_ID" type="java.lang.Long" unsaved-value="none"> <generator class="sequence"> </generator> </id> <property name="name" column="NAME" type="java.lang.String"/> <many-to-one name="belongTeam" class="team" cascade="save-update" column="TEAM_ID" /> </class> </hibernate-mapping> 测试代码 team t = (team);session.load(team.class,new Long(17););; // Player p = (Player);t.getPlayers();.iterator();.next();; Player p = new Player();; p.setName("!80887");; t.getPlayers();.add(p);; session.update(t);; 现在的问题是,如果用Player p=(Player)t.getPlayers().interator().next()这段代码那么无论Player.hbm.xml中unsaved-value设置为什么,那么都是update; 而,如果注释掉这段代码而用后面这段代码,那么无论Player.hbm.xml中unsaved-value设置为什么,那么都是insert; 从上面的测试来看,好象只要是新建的对象就是insert,load上来的就是update 怎么和坛主说的不一样呢 请坛主解释一下. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-01-16
引用 坛主水平真高,佩服! 楼上的兄弟,你是否听过"没有实践,就没有发言权" 这句话,你在没有实践之前,不要到处跟风,无论是自动分配的,还是手工分配的结果都是我说的那样 |
|
返回顶楼 | |
发表时间:2004-01-17
team t = (team);session.load(team.class,new Long(17););; // Player p = (Player);t.getPlayers();.iterator();.next();; Player p = new Player();; p.setName("!80887");; t.getPlayers();.add(p);; session.update(t);; 加上一句 // Player p = (Player);t.getPlayers();.iterator();.next();; Player p = new Player();; p.setName("!80887");; p.setTeam(t);; t.getPlayers();.add(p);; session.update(t);; 应该就可以了把 |
|
返回顶楼 | |
发表时间:2004-01-17
加上那句,是用来维护外键的,如果不加这句,那么外键就是null
所以加不加,都是上面的结果,只是外键的结果不一样罢了 |
|
返回顶楼 | |
发表时间:2004-01-17
所以我觉得unsaved-value只对save的时候起作用,而对其他操作无效
在有对于session.flush().这个语句我有点疑惑 team t = (team);session.load(team.class,new Long(18););; Player p = (Player);t.getPlayers();.iterator();.next();; // Player p = new Player();; p.setName("k6865kdkd");; t.getPlayers();.add(p);; // p.setBelongTeam(t);; session.flush();; 这种方式只要flush一下,而前面不用显式的调用update(),那么它就会知道你要做什么,并发送相应的语句 而对于save的时候,你必须在flush之前调用save那么他才知道你要做insert操作,所以我现在想问的是flush是怎么一个操作过程 |
|
返回顶楼 | |