Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

1.2.5. Bi-directional associations


Next you will map a bi-directional association. You will make the association between person and event work from both sides in Java. The database schema does not change, so you will still have many-to-many multiplicity.

Note

A relational database is more flexible than a network programming language, in that it does not need a navigation direction; data can be viewed and retrieved in any possible way.
First, add a collection of participants to the Event class:
    private Set participants = new HashSet();

    public Set getParticipants() {
        return participants;
    }

    public void setParticipants(Set participants) {
        this.participants = participants;
    }
Copy to Clipboard Toggle word wrap
Now map this side of the association in Event.hbm.xml.
<set name="participants" table="PERSON_EVENT" inverse="true">
  <key column="EVENT_ID"/>
  <many-to-many column="PERSON_ID" class="org.hibernate.tutorial.domain.Person"/>
</set>
Copy to Clipboard Toggle word wrap
These are normal set mappings in both mapping documents. Notice that the column names in key and many-to-many swap in both mapping documents. The most important addition here is the inverse="true" attribute in the set element of the Event's collection mapping.
What this means is that Hibernate should take the other side, the Person class, when it needs to find out information about the link between the two. This will be a lot easier to understand once you see how the bi-directional link between our two entities is created.
Nach oben
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2025 Red Hat