Jump to content

  • Log In with Google      Sign In   
  • Create Account

hibernate - one to many relationship in the same table.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 pythedev   Members   -  Reputation: 101

Like
0Likes
Like

Posted 21 April 2012 - 11:42 AM

Hello!

I have a posgresql database where information about rivers are stored.

CREATE TABLE river_data
(
  id_river integer,
  id_destination integer,
  river_name character varying(15),
... many other fields
)
WITH (
  OIDS=FALSE
);
ALTER TABLE river_data
  OWNER TO xyz;

Getting information about one single river without its subrivers works well:

@Entity
@Table(name = "river_data")
public class RiverData {
	@Id
	@Column (name = "id_river")
	private int riverId;
	@Column (name = "river_name")
	private String riverName;
	@Column (name = "id_destination")
	private int destinationRiverId;
	public int getRiverId() {
		return riverId;
	}
	public String getRiverName() {
		return riverName;
	}
	public int getDestinationRiverId() {
		return destinationRiverId;
	}
}

Now I want to retrieve information about a river with a list(collection) of its subrivers, but I don't know how. All subrivers should have their destination_id equal to the main river_id. The current code looks like this(it crashes the sql server):

@Entity
@Table(name = "river_data")
public class RiverDataWithSubRivers {
	@Id
	@Column (name = "id_river")
	private int riverId;
	@Column (name = "river_name")
	private String riverName;
	@Column (name = "id_destination")
	private int destinationRiverId;
	@OneToMany
	@JoinColumn(name="id_destination") // TODO: problem is here.
	private Collection<RiverData> subRivers= new ArrayList<RiverData>(10);
	public Collection<RiverData> getSubRivers() {
		return subRivers;
	}
	public int getRiverId() {
		return riverId;
	}
	public void setRiverId(int riverId) {
		this.riverId = riverId;
	}
	public String getRiverName() {
		return riverName;
	}
	public void setRiverName(String riverName) {
		this.riverName = riverName;
	}
	public int getDestinationRiverId() {
		return destinationRiverId;
	}
	public void setDestinationRiverId(int destinationRiverId) {
		this.destinationRiverId = destinationRiverId;
	}
}

The question is, what annotations and how should I use them in this part of code:

    @OneToMany
    @JoinColumn(name="id_destination") // TODO: problem is here.
    private Collection<RiverData> subRivers= new ArrayList<RiverData>(10);

My idea was that the @JoinColumn would help me to select all rivers, which have the same id_destination, that equal to the id_river of this class.

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS