5 Replies Latest reply: May 14, 2012 11:44 AM by user10373232 RSS

    eclipselink jpa association table mysql autoincrement problem

    user10373232
      Hi,
      I have problem inserting a record in the following scenario using Netbeans7.1-EclipseLink-Mysql
      I have 3 tables:

      A
      B
      A_B

      the id Autogenerated primary key in A and B are autoincrement primary key on mysql db and the primary key on the A_B table has given by the A.I and B.Id fields.

      The reverted generated entity classes are 4:

      AEntity
      BEntity
      A_BEntity
      A_BEntityPK

      all with the relatives annotation.....@ManyToOne etc etc

      I have tried to persist an AEntity Object Or a A_BEntityObject but I have always the same problem:

      The A_BEntityPK object is not inizialized.If I initialize it a new Constructor It loss the integrity with A.PK and B.PK and goes in intergrity violation.It seems like it can retrieve the autogerated A.Pk and B.Pk to populate the A_B.PK Object.

      Any ideas?

      thanks
        • 1. Re: eclipselink jpa association table mysql autoincrement problem
          Cdelahun-Oracle
          Hello,

          I'm not sure of how A_BEntityPK is related to the A_BEntity entity. Is it an embeddedId or a pkclass? If it is an embeddedId, you must instantiate it yourself, and populate the references from A_BEntity->A and A_BEntity->B yourself. Depending on how you have this mapped, that might mean getting the ID from A/B to put into the pk fields for A_BEntity. If using JPA 2.0, you can map it such that the A_BEntity->A and A_BEntity->B 1:1 relationships can be marked as @ID or @MapsId (if using an embeddedID). An example can be found here:
          http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers

          If the A_B table is a simple relation table though, you might not need the A_BEntity class and be able to map the A<->B relationship using a ManyToMany mapping.
          • 2. Re: eclipselink jpa association table mysql autoincrement problem
            user10373232
            Hi,
            thanks for the answer

            I have eliminated the embedded annotation strategy and followed your instruction.I have also studied a book,JPA 2.0 mastering etc etc,and used the PrimaryKey Class with the 2 id name like the annotated meber attribute in the relative 2 "sub-classes".Now,If I persist the A_B table with the 2 others object added I receive integrity constraint violation because the primary key from the first class is null ( A.primary_id and B.pimary_id are auto incremented ),I call the create method ( persist ) on the local interface of the stateless on the entity A_B .
            If I create the A entity ( create method oon the interface of the session on the entity A) it persists only the first entity on the first table,without association table and second table.
            Witch entity have I to persist?

            All the the A and B Entity instance are populated with the association A_B collection too.
            The A_B entity object has the two instance A and B object in it.

            Any ideas?
            • 3. Re: eclipselink jpa association table mysql autoincrement problem
              Cdelahun-Oracle
              How you set up your mappings and make the assocations is important. Please show your entities if you can.

              Simplest way is something like:
              @Entity
              @PKClass(ABPk.class)
              public classs A_BEntity {
              @ID
              @ManyToOne
              A a;
              @ID
              @ManyToOne
              B b;
              }

              You then need to make sure A_BEntity instances always reference an A and B before you persist it.
              • 4. Re: eclipselink jpa association table mysql autoincrement problem
                Cdelahun-Oracle
                It also sounds like you are expecting the persist operation to cascade to referenced entities. If so, this only occurs if cascade persist is specified on mappings - This someone describes it for a OneToMany, but the cascade option exists on all reference mappings:
                http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/OneToMany
                • 5. Re: eclipselink jpa association table mysql autoincrement problem
                  user10373232
                  thanks,

                  I signed bidirectional persistence cascade.So the entity I need to persist is the A_B.But are you sure it works with autoincrement pk too?