6 Replies Latest reply on Jan 4, 2008 8:41 AM by hiasi29

    How to define unique constraint on foreign keys ?

    chawax

      Hi,

      I need to define a unique constraint on foreign keys in a EJB3 entity.
      I saw the @Table annotation has a uniqueConstraints attribute to do this, but when I add relation columns to the unique constraint, it can't find them.

      For example, my entity has the following relations :

      @javax.persistence.ManyToOne(optional = false)
      @javax.persistence.JoinColumn(name = "NOEUD_ORGANISATION_FK")
      public xxx.core.utilisateur.NoeudOrganisation getNoeudOrganisation()
      {
       return this.noeudOrganisation;
      }
      
      @javax.persistence.ManyToOne(optional = false)
      @javax.persistence.JoinColumn(name = "EMPLOYE_FK")
      public xxx.core.utilisateur.Employe getEmploye()
      {
       return this.employe;
      }


      To define a unique constraint on these columns, I added the following annotation to the class :

      @javax.persistence.Entity
      @javax.persistence.Table(
       name = "UNITE_EMPLOYE",
       uniqueConstraints = {@javax.persistence.UniqueConstraint(columnNames = {"employe", "noeudOrganisation"})})
      public class UniteEmploye {
      }
      


      But I have the following error :
      ERROR 10-10 11:48:23,656 (KernelErrors.java:validate:78) -Failed deployment: persistence.units:jar=classes.jar,unitName=t4Seam
      org.hibernate.MappingException: Unable to find column with logical name: UNITE_EMPLOYE.employe
       at org.hibernate.cfg.Mappings.getPhysicalColumnName(Mappings.java:493)
       at org.hibernate.cfg.AnnotationConfiguration.buildUniqueKeyFromColumnNames(AnnotationConfiguration.java:488)
       at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:310)
       at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1039)
       at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1015)
       at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:751)
       at org.hibernate.ejb.Ejb3Configuration.createContainerEntityManagerFactory(Ejb3Configuration.java:350)
       at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:119)
       at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:264)
      


      How should I do this ?

        • 1. Re: How to define unique constraint on foreign keys ?
          chawax

          Anyone has an idea ?

          • 2. Re: How to define unique constraint on foreign keys ?

            According to spec you can use

            @javax.persistence.JoinColumn(name = "EMPLOYE_FK",unique=true)
            



            However, if you are creating the schema from scratch I suggest you use a @OneToOne relationship which does exactly what you want.

            Regards

            Felix

            • 3. Re: How to define unique constraint on foreign keys ?
              chawax

              Thanks. But what I need is a unique constraint on multiple columns, including columns that are foreign keys.

              Anyway, I solved my problem by using the attribute "name" of the JoinColumn annotation instead of the property name.

              @javax.persistence.Table(
               name = "UNITE_EMPLOYE",
               uniqueConstraints = {
               @javax.persistence.UniqueConstraint(columnNames = {"EMPLOYE_FK", "NOEUD_ORGANISATION_FK"})})
              


              What is strange is that in the Hibernate documentation, it is said :

              The @Table element also contains a schema and a catalog attributes, if they need to be defined. You can also define unique constraints to the table using the @UniqueConstraint annotation in conjunction with @Table (for a unique constraint bound to a single column, refer to @Column).

              @Table(name="tbl_sky",
              uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})}
              )

              A unique constraint is applied to the tuple month, day. Note that the columnNames array refers to the logical column names.


              Am I wrong when I think that logical column name is the property name of the entity bean ? Or maybe there's a difference between Hibernate and EJB3 entities about this ?

              • 4. Re: How to define unique constraint on foreign keys ?
                hiasi29

                Did you manage to solve this? I have the same problem.

                • 5. Re: How to define unique constraint on foreign keys ?
                  chawax

                  I solved this by using column names instead of properties names to define the unique constraint. It does not look logical to me, but it works.

                  • 6. Re: How to define unique constraint on foreign keys ?
                    hiasi29

                    Thanks, that worked for me, too!