1. Dashboard
  2. Forum
    1. Unerledigte Themen
  3. Mitglieder
    1. Letzte Aktivitäten
    2. Benutzer online
    3. Team-Mitglieder
    4. Trophäen
    5. Mitgliedersuche
  4. Tutorial Bereich
  • Anmelden
  • Registrieren
  • Suche
Dieses Thema
  • Alles
  • Dieses Thema
  • Dieses Forum
  • Seiten
  • Forum
  • Lexikon
  • Erweiterte Suche
  1. Informatik Forum
  2. Webmaster & Internet
  3. Entwicklung

Hibernate question: How to annotate a map of <object1, object2> in a m:m relationship

  • sommeralex
  • 2. November 2012 um 22:41
  • Unerledigt
  • sommeralex
    11
    sommeralex
    Mitglied
    Punkte
    1.325
    Beiträge
    188
    • 2. November 2012 um 22:41
    • #1

    Hello!

    I have two classes, the one is named Group, the other BaseFeature. now, i want that the class group is persisting several objects of the BaseFeature class as a map (list).

    @Entity(name = "`group`")
    public class Group {

    private Map<String, BaseFeature> baseFeatures;

    @ElementCollection
    public Map<String, BaseFeature> getBaseFeatures() {
    return baseFeatures;
    }
    }

    If am adding now several baseFeatures to a group X and persisting group X, it works. but if i am adding then the same baseFeatures to group Y, an exception is thrown:

    org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "group_BaseFeature_basefeatures_id_key" Detail: Key (basefeatures_id)=(12) already exists.

    Group_id [PK] bigint; basefeatures_id bigint; basefeatures_key [PK] character varying (255)
    90;12;"Age"
    90;10;"Emotion"
    90;13;"Gender"
    90;11;"Relationship"
    90;14;"Wealth"
    90;16;"WeatherDegree"
    90;15;"WeatherType"


    the generated table syntax looks like this, and i think that the problem is the last line, marked bold:


    CREATE TABLE "group_BaseFeature"
    (
    "Group_id" bigint NOT NULL,
    basefeatures_id bigint NOT NULL,
    basefeatures_key character varying(255) NOT NULL,
    CONSTRAINT "group_BaseFeature_pkey" PRIMARY KEY ("Group_id" , basefeatures_key ),
    CONSTRAINT fkfe462505436784f6 FOREIGN KEY (basefeatures_id)
    REFERENCES basefeature (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT fkfe46250546aa625f FOREIGN KEY ("Group_id")
    REFERENCES "group" (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT "group_BaseFeature_basefeatures_id_key" UNIQUE (basefeatures_id )
    )

    I dont understand why group_BaseFeature_basefeatures_id_key is generated as UNIQUE, nor, how to solve my problem. thx for any help..

    alex

    Einmal editiert, zuletzt von sommeralex (2. November 2012 um 22:51)

  • sommeralex
    11
    sommeralex
    Mitglied
    Punkte
    1.325
    Beiträge
    188
    • 3. November 2012 um 00:47
    • #2

    solution;

    @ManyToMany
    public Map<String, BaseFeature> getBaseFeatures() {
    return baseFeatures;
    }
    }

  • Maximilian Rupp 27. Dezember 2024 um 00:26

    Hat das Thema aus dem Forum Programmieren nach Entwicklung verschoben.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!

Benutzerkonto erstellen Anmelden

Benutzer online in diesem Thema

  • 1 Besucher

Rechtliches

Impressum

Datenschutzerklärung