Frage

I have a class City where some City objects will have a many-to-many relationship with other City objects. This relationship is a sister city relationship where, for example, the City object Tokyo can have a sister city relationship with the City objects, Helsinki and London, and London can be a sister city of Tokyo, Paris, and Minneapolis. I've read the documentation here : http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/ especially the 2.2.5.3.2.1. Definition section on Many-To-Many mapping. This is my City class:

@Entity
@Table(name = "city")
public class City {
@Id
@Column(name = "city_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long cityID;

@Column(name = "name_txt")
private String cityName;

@OneToOne
@JoinColumn(name = "type_id")
private CityType cityType;

@OneToOne
@JoinColumn(name = "state_id")
private State state;

@Column(name = "population_nbr")
 private int population;

@Column(name = "area_nbr")
private double area;

  /** Mapping Entity Associations */ 

@ManyToMany(
    targetEntity=City.class,
    cascade = CascadeType.PERSIST, fetch = FetchType.EAGER
)
@JoinTable(name="sister_city",
        joinColumns = @JoinColumn(name="city_id"),
        inverseJoinColumns = @JoinColumn(name="sister_city_id")
)
    private List<City> sisterCities;

public City() {
cityType = new CityType();
state = new State();
}
    //Getters and Setters ....

My association table is called sister_city and it has three columns, id, city_id, and sister_city_id. Right now I can create a new city, select the sister cities from previously created cities in a select box and have the city ids and chosen sister city ids added to the correct columns in the association table.

My problem is when I add new city Twig and give it the sister cities of Tokyo, Paris, and London, when I select for the sister cities of Paris, Twig is not selected along with the other sister cities. I've read about the mappedBy argument and how it designates the owner side property of a Many-To-Many relationship, but how do you do this when the objects are from the same class?

Right now if Paris has the city_id 18, and Twig 24, the city_id 18 -- sister_city_id 24 relationship is added, but the city_id 24 -- sister_city_id 18 relationship is not also automatically added through Hibernate.

War es hilfreich?

Lösung

I think you are running into the common problem that you must update each bi-directional relationship individually. These relationships do not happen automatically. So not only do you need to do an add of the cities Tokyo, Paris and London to Twig you must do the opposite as well, for each city you just added you must make the opposite relationship. In other words you must add Twig to Tokyo, Paris and London. Be sure to call an update after you set the relation for each city.

I don't believe that the mappedBy parameter would make sense here. For more info on the mappedBy parameter read this:

Can someone please explain mappedBy in hibernate?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top