Jump to content
  • Advertisement
Sign in to follow this  
pythedev

hibernate - one to many relationship in the same table.

This topic is 2281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!