Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes One to many Relation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "One to many Relation" Watch "One to many Relation" New topic
Author

One to many Relation

Ambily G Nair
Greenhorn

Joined: Aug 21, 2004
Posts: 28
Hi,

I 'm trying to do a sample hibernate code.

I have 2 tables Teams and Players in which primary key of Teams is the foreign key of Players.

My mapping documents are

<hibernate-mapping>
<class name="example.Player" table="players">
<id name="id" column="player_id" type="long" unsaved-value="null">
<generator class="sequence"/>
</id>
<property name="firstName" column="first_name" type="string" length="12" not-null="true"/>
<property name="lastName" column="last_name" type="string" length="15" not-null="true"/>
<property name="draftDate" column="draft_date" type="date"/>
<property name="annualSalary" column="salary" type="float"/>
<property name="jerseyNumber" column="jersey_number" type="integer" length="2" not-null="true"/>
<many-to-one name="team" class="example.Team" column="team_id" />
</class>
</hibernate-mapping>


<hibernate-mapping>
<class name="example.Team" table="teams">
<id name="id" column="team_id" type="long" unsaved-value="null">
<generator class="sequence"/>
</id>
<property name="name" column="team_name" type="string"
length="15" not-null="true"/>
<property name="city" column="city" type="string" length="15" not-null="true"/>
<set name="players" cascade="all" inverse="true" lazy="true">
<key column="team_id"/>
<one-to-many class="example.Player"/>
</set>
</class>
</hibernate-mapping>


My persistence classes are

public class Team implements Serializable {

/** identifier field */
private Long id;

/** persistent field */
private String name;

/** persistent field */
private String city;

/** persistent field */
private Set players;

/** full constructor */
public Team(String name, String city, Set players) {
this.name = name;
this.city = city;
this.players = players;
}

/** default constructor */
public Team() {
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getCity() {
return this.city;
}

public void setCity(String city) {
this.city = city;
}

public Set getPlayers() {
return this.players;
}

public void setPlayers(Set players) {
this.players = players;
}

public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof Team) ) return false;
Team castOther = (Team) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}

}



public class Player implements Serializable {

/** identifier field */
private Long id;

/** persistent field */
private String firstName;

/** persistent field */
private String lastName;

/** nullable persistent field */
private Date draftDate;

/** nullable persistent field */
private float annualSalary;

/** persistent field */
private int jerseyNumber;

/** nullable persistent field */
private example.Team team;

/** full constructor */
public Player(String firstName, String lastName, Date draftDate, float annualSalary, int jerseyNumber, example.Team team) {
this.firstName = firstName;
this.lastName = lastName;
this.draftDate = draftDate;
this.annualSalary = annualSalary;
this.jerseyNumber = jerseyNumber;
this.team = team;
}

/** default constructor */
public Player() {
}

/** minimal constructor */
public Player(String firstName, String lastName, int jerseyNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.jerseyNumber = jerseyNumber;
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return this.lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Date getDraftDate() {
return this.draftDate;
}

public void setDraftDate(Date draftDate) {
this.draftDate = draftDate;
}

public float getAnnualSalary() {
return this.annualSalary;
}

public void setAnnualSalary(float annualSalary) {
this.annualSalary = annualSalary;
}

public int getJerseyNumber() {
return this.jerseyNumber;
}

public void setJerseyNumber(int jerseyNumber) {
this.jerseyNumber = jerseyNumber;
}

public example.Team getTeam() {
return this.team;
}

public void setTeam(example.Team team) {
this.team = team;
}

public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof Player) ) return false;
Player castOther = (Player) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}

}

In my code I'm trying to do an insert into 2 tables as below



team.setCity("Detroit");
team.setName("Pistons");
Player player = new Player();
player.setFirstName("Chauncey");
player.setLastName("Billups");
player.setJerseyNumber(1);
player.setAnnualSalary(4000000f);
Set players = new HashSet();
players.add(player);
team.setPlayers(players);
session.save(team);
session.flush();
session.connection().commit(); // not necessary for JTA datasource
session.close();

But after executing this I'm getting a record in TEAMS as expected. But in PLAYERS the foreign key column ie TEAM_ID is null.
Why is it so.Can anyone help me?

Ambily
sanasdf kumar
Greenhorn

Joined: Aug 30, 2005
Posts: 1
We were facing the same problem. You need to add Team in Player before saving team.

player.setTeam(team);



//your code snippet
players.add(player);
team.setPlayers(players);
player.setTeam(team);
session.save(team);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: One to many Relation
 
Similar Threads
One to Many && Many to One mapping
could not initialize a collection+ [Microsoft][ODBC SQL Server Driver]Invalid Descrip
Why does EJB assume a join table and how do i get around it?
[JPA - Hibernate] OneToMany without join table
Error: "Could not synchronize database state with session"