嗨,我告诉你我有一个Team类,其中包含我使用方法getPlayers()获得的Set Player类型的对象列表。但是这个列表总是很快,虽然Team对象本身不会在通过get方法加载后甚至加载会话对象(Session.get(Team.class,new Integer(1))。我注意到即使数据库远非空白。
你能帮帮我吗?Team.java
public class Team {
private int idteam;
private String nom;
private Set<Player> players = new HashSet<Player>();
private Coach coach;
public Team (){
super();
}
public Team(int team_Id, String nom,Coach coach, Set<Player> players) {
super();
idteam = team_Id;
this.nom = nom;
this.players = players;
this.coach = coach;
}
public Team(int team_Id, String nom,Coach coach) {
super();
idteam = team_Id;
this.nom = nom;
this.players = players;
this.coach = coach;
}
public int getidteam() {
return idteam;
}
public void setidteam(int team_Id) {
idteam = team_Id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Set<Player> getPlayers() {
return players;
}
public void setPlayers(Set<Player> players) {
this.players = players;
}
public Coach getCoach() {
return coach;
}
public void setCoach(Coach coach) {
this.coach = coach;
}
}
Player.java
public class Player {
private int Player_Id;
private Team team;
private String nom;
private String prenom;
public Player(int player_Id, String nom, String prenom, Team team) {
super();
Player_Id = player_Id;
this.nom = nom;
this.prenom = prenom;
this.team=team;
}
public Player(int player_Id, String nom, String prenom) {
super();
Player_Id = player_Id;
this.nom = nom;
this.prenom = prenom;
}
public Player() {
super();
}
public int getPlayer_Id() {
return Player_Id;
}
public void setPlayer_Id(int player_Id) {
Player_Id = player_Id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
public String toString(){
return " " +this.nom + " " + this.prenom + " |";
}
}
player.hbm.xml
<class name="foot.Player" table="PLAYER">
<id name="Player_Id" column="ID_PLAYER">
<generator class="native" />
</id>
<property name="nom">
<column name="NOM" />
</property>
<property name="prenom">
<column name="PRENOM" />
</property>
<many-to-one name="team" class="foot.Team" column="ID_TEAM"
cascade="all"></many-to-one>
</class>
team.hbm.xml
<class name="foot.Team" table="TEAM">
<id name="idteam" column="ID_TEAM">
<generator class="native" />
</id>
<property name="nom">
<column name="NOM" />
</property>
<many-to-one name="coach" class="foot.Coach" column="ID_COACH"
cascade="all" not-null="true" />
<set name="players" cascade="all" inverse="true">
<key column="ID_TEAM" not-null="true" />
<one-to-many class="foot.Player" />
</set>
</class>
main.java
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Coach coach = new Coach(1, "galles", "charles");
Team team1 = new Team(1, "france", coach);
Player p1 = new Player(1, "anelka", "nicolas");
Player p2 = new Player(2, "henry", "thierry");
p1.setTeam(team1);
session.merge(p1);
p2.setTeam(team1);
session.merge(p2);
Team p = (Team) session.get(Team.class, new Integer(1));
System.out.println(p.getNom() + " " + p.getCoach().getNom() + " "
+ p.getPlayers().size());
session.flush();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
HibernateUtil.getSessionFactory().close();
PS:我使用HIBERNATE 4.3.5 FINAL
感谢。