-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Unidirectional One-To-Many relationship
PostPosted: Thu Mar 26, 2009 10:44 am 
Newbie

Joined: Thu Mar 26, 2009 10:33 am
Posts: 2
Hi,

I am trying to create a relation between my classes Cell and TeamPoint using Hibernate. Each Cell is supposed to be referred to by many TeamPoints.

When i create a Cell consisting of a list of e.g. two TeamPoints, the Cell is correctly created in the database, but the TeamPoints are just created with their cellId=NULL. I thought that this should be updaed by Hibernate after insertion?

The important parts of the two classes is shown below. I have left out unrealted fields, getters and setters, from both classes.

Cell:
Code:
@Entity
@Table(name = "cell")
public class Cell implements Serializable{
   @Id @GeneratedValue(strategy = GenerationType.AUTO)
   int id;

   @OneToMany(
      mappedBy="cellId",
      cascade = CascadeType.ALL,
      fetch = FetchType.EAGER,
      targetEntity=TeamPoint.class
   )
   @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
   @OrderBy("teamId")
   List<TeamPoint> teamPoints = new LinkedList<TeamPoint>();

   public Cell() {
   }

   public List<TeamPoint> getTeamPoints() {
      return teamPoints;
   }

   @OneToMany
   @JoinColumn (name = "cellid")
   public void setTeamPoints(List<TeamPoint> teamPoints) {
      this.teamPoints = teamPoints;
   }
}

TeamPoint:
Code:
@Entity
@Table(name = "teamPoints")
public class TeamPoint implements Serializable {
   @Id @GeneratedValue(strategy = GenerationType.AUTO)
   int id;
   @ManyToOne(
      cascade = {CascadeType.PERSIST, CascadeType.MERGE},
      targetEntity=Cell.class
   )
   @JoinColumn (name="cellId", nullable = false, updatable = false, insertable = false)
   Cell cellId;
   int teamId;
   float points;

   public TeamPoint() {
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 27, 2009 6:30 am 
Newbie

Joined: Thu Mar 26, 2009 10:33 am
Posts: 2
Maybe the problem is how i construct the objects?
Code:
   Cell cell = new Cell();
   List<TeamPoint> teamPoints = new LinkedList();
   teamPoints.add(new TeamPoint(1, 51));
   teamPoints.add(new TeamPoint(2, 49));
   cell.setTeamPoints(teamPoints);
   cell.commit(); //Save & commit  object to DB


As i said, both the Cell and two TeamPoint objects are stored in the DB, but the TeamPoints' cellId is not set to refer to the Cell, it is just NULL.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 27, 2009 7:39 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
From your code it seems like you are using a bi-directional association, not a uni-directional as stated in the title.

You'll need to call TeamPoint.setCell() (add that method if you don't have it already) since this is the owning side of the association.

Everything is explained in this example in the Hibernate documentation: http://www.hibernate.org/hib_docs/v3/re ... bidir.html
The example is using xml mappings, but the reasoning is the same for annotations.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.