hibernate问题,在线等
<?xml version= "1.0 " encoding= "utf-8 "?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN "
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name= "pojo.Player " table= "player " schema= "dbo " catalog= "NBA ">
<id name= "pid " type= "java.lang.Integer ">
<column name= "pid " />
<generator class= "native " />
</id>
<property name= "pname " type= "java.lang.String ">
<column name= "pname " length= "20 " not-null= "true " />
</property>
<many-to-one name= "team " class= "pojo.Team " column= "tid "> </many-to-one>
</class>
</hibernate-mapping>
package pojo;
/**
* Player generated by MyEclipse - Hibernate Tools
*/
public class Player implements java.io.Serializable {
// Fields
private Integer pid;
private String pname;
private Team team;
// Constructors
/** default constructor */
public Player() {
}
/** full constructor */
public Player(String pname, Team team) {
this.pname = pname;
this.team = team;
}
// Property accessors
public Integer getPid() {
return this.pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return this.pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
}
<?xml version= "1.0 " encoding= "utf-8 "?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN "
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name= "pojo.Team " table= "team " schema= "dbo " catalog= "NBA ">
<id name= "id " type= "java.lang.Integer ">
<column name= "id " />
<generator class= "native " />
</id>
<property name= "tid " type= "java.lang.Integer ">
<column name= "tid " not-null= "true " />
</property>
<property name= "tname " type= "java.lang.String ">
<column name= "tname " length= "30 " not-null= "true " />
</property>
<set name= "players " table= "player " cascade= "all " inverse= "true ">
<key column= "tid "> </key>
<one-to-many class= "pojo.Player "/>
</set>
</class>
</hibernate-mapping>
package pojo;
import java.util.HashSet;
import java.util.Set;
/**
* Team generated by MyEclipse - Hibernate Tools
*/
public class Team implements java.io.Serializable {
// Fields
private Integer id;
private Integer tid;
private String tname;
private Set players = new HashSet();
// Constructors
/** default constructor */
public Team() {
}
/** full constructor */
public Team(Integer tid, String tname) {
this.tid = tid;
this.tname = tname;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getTid() {
return this.tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return this.tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public Set getPlayers() {
return players;
}
public void setPlayers(Set players) {
this.players = players;
}
}
[解决办法]
按你的配置,应该先把Team对象持久了,再持久Player对象,因为你用了
<set name= "players " table= "player " cascade= "all " inverse= "true ">
<key column= "tid "> </key>
<one-to-many class= "pojo.Player "/>
</set>
当然,你也可以加上inverse= "true "
[解决办法]
参考我的博客
http://blog.csdn.net/kipen/archive/2007/04/05/1553448.aspx