1 Reply Latest reply on Jun 4, 2010 1:43 PM by sridhar kondoji

    OneToOne bidirectional with foreign key referencing Non primary key

    sridhar kondoji Newbie
      CREATE TABLE IF NOT EXISTS HOSTS
      (
              hostId INT UNSIGNED NOT NULL AUTO_INCREMENT,
              id VARCHAR(255) NOT NULL,
              PASSWORD VARCHAR(255) NOT NULL,
              enabled tinyint(1) NOT NULL DEFAULT 0,
      
              version INT UNSIGNED NOT NULL DEFAULT 0,
              UNIQUE (hostId),
              PRIMARY KEY(id)
              
      );
      
      
      CREATE TABLE IF NOT EXISTS PROFILES
      (
              id INT UNSIGNED NOT NULL,
              hostName VARCHAR(255) default NULL,     
              lastloginDate DATETIME NOT NULL,
              lastupdated TIMESTAMP DEFAULT NOW() on UPDATE NOW(),
      
              accountVerificationDate DATETIME NOT NULL,      
              registeredDate DATETIME NOT NULL,
      
              version INT UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY(id),
              CONSTRAINT FOREIGN KEY(id) REFERENCES HOSTS(hostId)
      );




      
      @Entity
      @Table(name = "hosts", catalog = "ebulawa")
      public class Hosts implements Cloneable, Serializable {
              /** Serial Version UID. */
              private static final long serialVersionUID = -559016049L;
              /** Use a WeakHashMap so entries will be garbage collected once all entities
                      referring to a saved hash are garbage collected themselves. */
              private static final Map<Serializable, String> SAVED_HASHES =
                      Collections.synchronizedMap(new WeakHashMap<Serializable, String>());
      
              /** hashCode temporary storage. */
              private volatile String hashCode;
      
      
              /** Field mapping. */
              private Boolean enabled;
              /** Field mapping. */
              private Long hostid;
              /** Field mapping. */
              private String id;
              /** Field mapping. */
              private String password;
      
      
      
      
              /** Field mapping. */
              private Long version;
      
          @OneToOne(mappedBy="host")
          public Profiles getProfile() {
              return profile;
          }
      
          public void setProfile(Profiles profile) {
              this.profile = profile;
          }
      
          /** Field mapping **/
          private Profiles profile;
      
              /**
               * Default constructor, mainly for hibernate use.
               */
              public Hosts() {
                      // Default constructor
              }
      
      
      ...
      }
      
      @Entity
      @Table(name = "profiles", catalog = "ebulawa")
      public class Profiles implements Cloneable, Serializable {
      
              /** Serial Version UID. */
              private static final long serialVersionUID = -559016045L;
              /** Use a WeakHashMap so entries will be garbage collected once all entities
                      referring to a saved hash are garbage collected themselves. */
              private static final Map<Serializable, String> SAVED_HASHES =
                      Collections.synchronizedMap(new WeakHashMap<Serializable, String>());
      
              /** hashCode temporary storage. */
              private volatile String hashCode;
      
      
      
          @OneToOne(cascade=CascadeType.ALL, optional=false)
          @JoinColumn(name="id", referencedColumnName = "hostId", unique=true)
          public Hosts getHost() {
              return host;
          }
      
          public void setHost(Hosts host) {
              this.host = host;
          }
      
          private Hosts host;
      
      
          @Id
          @GeneratedValue(generator="foreign")
          @GenericGenerator(name="foreign", strategy="foreign", parameters = {@Parameter(name="property", value="host")}
          )    
          @Column( nullable = false)
          public Long getId() {
              return id;
          }
      
          public void setId(Long id) {
              this.id = id;
          }
      
          /** Field mapping **/
          private Long id;
      ....
      }
      




      I have shown only relevant parts here.


      Is it possible to force the generator on Profiles entity to get the value from Hosts unique column (hostId) rather than primary key column which is of String type?


      I am getting Mismatch of types errors when persisting Profiles entity.


      My sample client code to persist host is







                     Hosts h = new Hosts();
                      h.setId("kkkchary@yahoo.com");
                      h.setEnabled(false);
                      h.setPassword(XYZ);
                      
                      Profiles p = new Profiles();
                      p.setHost(h);
                      regDao.createHost(h);





      Thanks
      Sri