0 Replies Latest reply on Sep 4, 2007 6:31 AM by Mads Henderson

    Problem with @OneToOne relations

    Mads Henderson Newbie

      In my application I have a one-to-one relation between my user entity and my address entity.

      Addresses are loaded into the system from an external source, afterwards users are created manually and the correct address is then associated with the user.

      My problem is that I am allowed to attach the same address to multiple users, without any problems saving the user entity. When trying to access the user again I get an error because the one-to-one relation has been violated. Is there a way to use annotations to ensure that the uniqueness is not violated so that I get a validation error when trying to persist or update my user entity, rather than having the application to fall apart because the datamodel was corrupted?

      In my entity classes I have the following.
      User:
      @OneToOne(optional = true)
      @JoinColumn(name = "address_id", nullable = true, unique = true)
      public Address getAddress()
      {
      return address;
      }

      Address:
      @OneToOne(mappedBy = "address")
      public Xuser getXuser() {
      return xuser;
      }

      public void setXuser(Xuser xuser) {
      this.xuser = xuser;
      }

      Thanks
      Mads