These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping complex association
PostPosted: Wed Aug 29, 2012 8:32 am 
Newbie

Joined: Wed Aug 29, 2012 7:52 am
Posts: 1
I am new in hibernate and i want to generate entities corresponding to following MySql schema :

Code:
CREATE TABLE IF NOT EXISTS `PARAM_TABLE_VERSIONS`
   ( `PARAM_TABLE_ID`               CHAR(2)         NOT NULL
   , `VERSION`                     VARCHAR(4)      NOT NULL
   , `CREATION_DATETIME`            DATETIME      NOT NULL
   , PRIMARY KEY (`PARAM_TABLE_ID`,`VERSION`)
   )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

CREATE TABLE IF NOT EXISTS `PARAM_CONTRACTS`
   ( `PARAM_CONTRACT_ID`            BIGINT(20)      NOT NULL
   , `ACCEPTOR_CONTRACT_ID`         CHAR(18)      NOT NULL
   , `ACTIVATION_DATETIME`            DATETIME      NOT NULL
   , `ACTIVE`                     TINYINT(1)      NOT NULL
   , `MANAGED`                     TINYINT(1)      NOT NULL
   , PRIMARY KEY (`PARAM_CONTRACT_ID`)
   , UNIQUE KEY `IDX_PARAM_CONTRACTS_UK` (`ACCEPTOR_CONTRACT_ID`,`MANAGED`,`ACTIVE`,`ACTIVATION_DATETIME`)
   )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

CREATE TABLE IF NOT EXISTS `PARAM_TABLES_CONTRACTS`
   ( `PARAM_CONTRACT_ID`            BIGINT(20)      NOT NULL
   , `PARAM_TABLE_ID`               CHAR(2)         NOT NULL
   , `CREATION_DATETIME`            DATETIME      NOT NULL
   , `VERSION`                     VARCHAR(4)      NOT NULL
   , PRIMARY KEY (`PARAM_CONTRACT_ID`,`PARAM_TABLE_ID`)
   , KEY `FK_TBLS_CONTRACTS_TBL_VERSIONS` (`PARAM_TABLE_ID`,`VERSION`)
   , KEY `FK_TABLES_CONTRACTS_CONTRACTS` (`PARAM_CONTRACT_ID`)
   , CONSTRAINT `FK_TABLES_CONTRACTS_CONTRACTS` FOREIGN KEY (`PARAM_CONTRACT_ID`) REFERENCES `PARAM_CONTRACTS` (`PARAM_CONTRACT_ID`)
   , CONSTRAINT `FK_TBLS_CONTRACTS_TBL_VERSIONS` FOREIGN KEY (`PARAM_TABLE_ID`, `VERSION`) REFERENCES `PARAM_TABLE_VERSIONS` (`PARAM_TABLE_ID`, `VERSION`)
   )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;


I put only necessary columns but each table have more columns.

I declare entities like that :

Code:
@Entity
@Table(name = "PARAM_TABLE_VERSIONS")
public class ParamTableVersion {

   @Embeddable
   public static class ParamTableVersionId implements java.io.Serializable {
      private static final long serialVersionUID = -5647192722679213011L;

      private String tableId;
      private String version;

      public ParamTableVersionId() {
      }

      public ParamTableVersionId(String tableId, String version) {
         this.tableId = tableId;
         this.version = version;
      }

      @Column(name = "PARAM_TABLE_ID", columnDefinition = "CHAR(2)", nullable = false)
      public String getTableId() {
         return this.tableId;
      }

      public void setTableId(String tableId) {
         this.tableId = tableId;
      }

      @Column(name = "VERSION", length = 4, nullable = false)
      public String getVersion() {
         return this.version;
      }

      public void setVersion(String version) {
         this.version = version;
      }
   }

   private ParamTableVersionId paramTableVersionId;
   private Date creationDatetime;

   private List<ParamTableContract> listParamTableContract;

   public ParamTableVersion() {
      this.paramTableVersionId = new ParamTableVersionId();
   }

   public ParamTableVersion(String tableId, String version) {
      this.paramTableVersionId = new ParamTableVersionId(tableId, version);
   }

   public ParamTableVersion(String tableId, String version, Date creationDatetime) {
      this.paramTableVersionId = new ParamTableVersionId(tableId, version);
      this.creationDatetime = creationDatetime;
   }

   @EmbeddedId
   public ParamTableVersionId getParamTableVersionId() {
      return this.paramTableVersionId;
   }

   public void setParamTableVersionId(ParamTableVersionId paramTableVersionId) {
      this.paramTableVersionId = paramTableVersionId;
   }

   @Column(name = "CREATION_DATETIME", nullable = false)
   @Temporal(TemporalType.TIMESTAMP)
   public Date getCreationDt() {
      return creationDatetime;
   }

   public void setCreationDt(Date creationDatetime) {
      this.creationDatetime = creationDatetime;
   }
}



Code:
@Entity
@Table(name = "PARAM_CONTRACTS", uniqueConstraints = { @UniqueConstraint(columnNames = {
      "ACCEPTOR_CONTRACT_ID", "MANAGED", "ACTIVE", "ACTIVATION_DATETIME" }) })
public class ParamContract {

   private Long paramContractId;
   private String acceptorContractId;
   private Boolean managed = true;
   private Boolean active = true;
   private Date activationDatetime;

   /**
    * Set the automatic Id
    *
    * @see ParamContract#setAcceptorContractId(String)
    */
   @PrePersist
   public void setAutomaticId() {
      setParamContractId(IDGenerator.getLongId());
   }

   @Id
   @Column(name = "PARAM_CONTRACT_ID", nullable = false)
   public Long getParamContractId() {
      return paramContractId;
   }

   public void setParamContractId(Long paramContractId) {
      this.paramContractId = paramContractId;
   }

   @Column(name = "ACCEPTOR_CONTRACT_ID", columnDefinition = "CHAR(18)", nullable = false)
   public String getAcceptorContractId() {
      return acceptorContractId;
   }

   public void setAcceptorContractId(String acceptorContractId) {
      this.acceptorContractId = acceptorContractId;
   }

   @Column(name = "MANAGED", nullable = false)
   public Boolean getManaged() {
      return managed;
   }

   public void setManaged(Boolean managed) {
      this.managed = managed;
   }

   @Column(name = "ACTIVE", nullable = false)
   public Boolean getActive() {
      return active;
   }

   public void setActive(Boolean active) {
      this.active = active;
   }

   @Column(name = "ACTIVATION_DATETIME", nullable = false)
   @Temporal(TemporalType.TIMESTAMP)
   public Date getActivationDatetime() {
      return activationDatetime;
   }

   public void setActivationDatetime(Date activationDatetime) {
      this.activationDatetime = activationDatetime;
   }
}


I want to know how declare the entity ParamTableContract for table PARAM_TABLES_CONTRACTS if i want bidirectional mapping in all entities.

please note that the VERSION column of the primary key of table PARAM_TABLE_VERSIONS is not part of PARAM_TABLES_CONTRACTS primary key.
thank for your answer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.