Sign in to follow this  

hibernate - one to many relationship in the same table.

This topic is 2065 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.

[CODE]
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;
[/CODE]

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

[CODE]
@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;
}
}
[/CODE]

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):

[CODE]
@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;
}
}
[/CODE]

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

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

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

This topic is 2065 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this