6 Replies Latest reply on Oct 17, 2008 12:58 AM by mark_man

    Problem with composite Primary key which is also a foreign k

    imranpariyani

      hello,

      I have a entity like:

      public class Parent implements Serializable {

      @OneToMany(cascade =CascadeType.ALL, mappedBy = "site", fetch=FetchType.EAGER)
      @OrderBy("position ASC")
      @Sort(type = SortType.NATURAL)
      private SortedSet childs;

      // method for adding the child object :
      public void addChild(Child child) {
      if(this.childs==null)
      this.childs=new TreeSet();
      child.set(this);
      this.childs.add(child);
      }
      }

      and the child class as follows:

      @Entity
      @IdClass(ChildPK.class)
      @Table(name = "child")
      public class Child implements Serializable, Comparable {
      @Id
      private int parentId;

      @Id
      private Language language;

      @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
      @JoinColumn(name = "parent_id",nullable = false,insertable = false, updatable = false, referencedColumnName = "id", columnDefinition = "parent_id INT NOT NULL")
      private Parent parent;
      }


      And the primary key class as follows :

      public final class ChildPK implements Serializable {
      @Column(name = "parent_id",insertable = false, updatable = false, nullable = false)
      private int parentId;

      @Enumerated(EnumType.STRING)
      private Language language;

      public boolean equals(Object other) {.....}

      }




      now the problem is if i am inserting a new parent with a new child .. it inserts the parent and then tries to insert the child with the parentId(foreign key) as 0 ...

      in other case where there is no composite key it inserts the parent and then gets the autogenerated parent id and assigns it the child and then inserts the child .. why dosent that happen with the child with composite key.

      What am i doing worng?

      Any help would be appreciated

      Regards

      Imran


        • 1. Re: Problem with composite Primary key which is also a forei

          Either your pk is ChildPK.class or it is an id. It cannot be both and you can not have two @ids.

          Regards

          Felix

          • 2. Re: Problem with composite Primary key which is also a forei
            imranpariyani

            Thanks for the fast reply ...

            The primary key for my example is ChildPK.class and according to the ejb3 specification i have the annotation @IdClass(ChildPK.class) in my child class

            all the columns in the pk class have to be present in the child class with @Id annotation so there will be two @Id annotations coz of the composite key..

            the one you are talking about is the @Embeddable annotation. I am not using that.

            Thanks and Regards

            Imran

            • 3. Re: Problem with composite Primary key which is also a forei
              oskar.carlstedt

              Hi!

              The @Id annotation will annotate the primary key of your class. The parent id is not the primary key of your class, it is a foreign key, but an @Id in the parent class. Remove @Id from the parentId.

              If you want to use composite keys, take a look here http://docs.jboss.org/ejb3/app-server/tutorial/composite/composite.html


              Kind Regards
              Oskar

              • 4. Re: Problem with composite Primary key which is also a forei
                imranpariyani

                I think i dint explain my problem that well ... will do it again

                CREATE TABLE `parent` (
                `id` int(11) NOT NULL auto_increment,
                `sitename` varchar(40) NOT NULL,
                PRIMARY KEY (`id`))

                CREATE TABLE `child` (
                `parent_id` int(11) NOT NULL,
                `language` enum('EN,'DE','FR') NOT NULL default 'EN',
                PRIMARY KEY (`parent_id`,`language`),
                CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE CASCADE
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8

                so as you see the parent_id in the child is a part of composite primary key and also a forigen key ..

                so the childPk has the fileds parentId and language ...
                and the child class must have the same exact properties as the childPk class and these properties are annotated with multiple @Id annotations..

                everything works fine .. like update delete insert .. there is only a prob when i try to insert a new parent .. with the child collection initialized ..

                If i first insert the parent with the child collection null and the insert the child collection after assigning the parent object which was inserted then it works fine .. but not if i insert new parent with child objects ... it throws this error of foreign key violation because it tries to insert the child with the foreign key parent id as 0

                Regards,

                Imran

                • 5. Re: Problem with composite Primary key which is also a forei

                  Create a PkClass that has a parent object and a String, like so:

                  @Embeddable
                  public class Child {
                  
                   private Parent parent;
                  
                   private String language;
                  
                  
                   // Getters and Setters
                  
                  }
                  


                  • 6. Re: Problem with composite Primary key which is also a forei

                    imranpariyani - you have articulated this issue very well. I am having the same problem with JBoss 4.2.2. Did you ever find a solution.

                    Thanks,
                    Mark