3 Replies Latest reply on Sep 3, 2007 9:03 AM by barondodd

    Can not seem to name foreign keys in relationships

      I am having problems with EJB3 bidirectional relationships. We are upgrading an EJB2 system to EJB3 and the names of our foreign key columns are causing us a headache.

      Table ArchiveSet has a foreign key into table Catalogue via the column CATALOGUEFK
      So we need a many-to-1 relationship.

      I create that side of the relationship and specify the name of the foreign key....

      @Entity
      public class ArchiveSet ...
      
       @ManyToOne
       @JoinColumn(name="catalogueFK")
       public Catalogue getCatalogue() {
       return catalogue;
       }


      I then complete the relationship and map to the catalogue property of the ArchiveSet entity...
      @Entity
      public class Catalogue ...
      
       @OneToMany(mappedBy="catalogue")
       public Collection<ArchiveSet> getArchiveSets() {
       return archiveSets;
       }


      The following error occurs:

      org.hibernate.MappingException: Unable to find column with logical name catalogue in table ArchiveSet

      We have tried everything and it will only stop moaning if both the JoinColumn name and the mappedBy name are "catalogue". The @Column annotation is not allowed in conjunction with the @ManyToOne annotation (don't know where this is documented...), so how on earth do I specify a different name for the foreign key?!! This could be a blocker to using EJB3.


        • 1. Re: Can not seem to name foreign keys in relationships
          lafr

          did you try to specify the name with referencedColumnName:
          @JoinColumn(name="catalogueFk",referencedColumnName="catalogue")

          • 2. Re: Can not seem to name foreign keys in relationships

            Thanks for the suggestion, unfortunately it did not solve the issue:

            org.hibernate.MappingException: Unable to find column with logical name: catalogue in org.hibernate.mapping.Table(Catalogue) and its related supertables and secondary tables


            From what we have learnt we know the following:

            a) The mappedBy attribute must reference the logical name of the foreign property (the name of the attribute / getter)
            b) The name attribute of the JoinColumn appears to be the physical column name for the foreign key
            c) It only works if these two values are the same, but not if that value is anything other than the name of the getter/setter property.

            Which brings us back to square 1! How do we specify a different physical name for the foreign key?!

            • 3. Re: Can not seem to name foreign keys in relationships

              Fixed it, it was the precence of the following that was causing the problem:

              @UniqueConstraint(columnNames={"name","catalogue"}

              Should have been

              @UniqueConstraint(columnNames={"name","catalogueFK"}