6 Replies Latest reply: Jan 4, 2008 8:41 AM by Matthias Gladt RSS

    How to define unique constraint on foreign keys ?

    Olivier Thierry Apprentice

      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 ?