4 Replies Latest reply: Jun 8, 2012 9:36 AM by 680728 RSS

    JPA: model a bidirectional tree with edges attributes

    680728
      Good day. I have a problem. I can't proper annotate my model. My aim is to model a bidirectional tree which edges do have attributes (some kind of weights). The other fact is that there are multiple trees stored in one table. They are distinguished by a special column.
      One leaf can have several parents and children...

      So:
      1. There are several trees in one table ETL_WORKFLOW_SEQ_REL distinguished by JOB_ID
      2. A leaf can have several parents or children
      3. An edge of the tree does have attributes.


      Here is my database:

      --It's a leaf of a tree. It's @Id=WORKFLOW_SEQ_ID
      CREATE MULTISET TABLE ETL_WORKFLOW_SEQ ,NO FALLBACK ,
      NO BEFORE JOURNAL,
      NO AFTER JOURNAL,
      CHECKSUM = DEFAULT,
      DEFAULT MERGEBLOCKRATIO
      (
      WORKFLOW_SEQ_ID INTEGER NOT NULL,
      WORKFLOW_ID BIGINT NOT NULL,
      IS_NAME VARCHAR(255) CHARACTER SET UNICODE NOT CASESPECIFIC,
      INFO_SYSTEM_INST_CD VARCHAR(255) CHARACTER SET UNICODE NOT CASESPECIFIC,
      DISABLE BYTEINT)
      UNIQUE PRIMARY INDEX ( WORKFLOW_SEQ_ID );

      --Here are my nodes:
      --PrimaryKey should be: WORKFLOW_SEQ_ID (child)+REL_WORKFLOW_SEQ_ID (parent)+JOB_ID (tree id)
      CREATE MULTISET TABLE ETL_WORKFLOW_SEQ_REL ,NO FALLBACK ,
      NO BEFORE JOURNAL,
      NO AFTER JOURNAL,
      CHECKSUM = DEFAULT,
      DEFAULT MERGEBLOCKRATIO
      (
      WORKFLOW_SEQ_ID INTEGER NOT NULL, --It's a child, it's a FK->ETL_WORKFLOW_SEQ 
      REL_WORKFLOW_SEQ_ID INTEGER NOT NULL, --It's a parent, it's a FK->ETL_WORKFLOW_SEQ 
      START_TYPE_ID SMALLINT NOT NULL, -- attribute of an edge
      DISABLE_START_TYPE_ID SMALLINT, -- attrbiute of an edge
      JOB_ID BIGINT) -- it's a determinant. This table keeps several trees.
      -- If you want to get a tree you need to select it's nodes using specified job_id.
      PRIMARY INDEX ( WORKFLOW_SEQ_ID );

      I've tried to model it...
      /** ETL_WORKFLOW_SEQ was easy*/
      @Entity
      @Table(name="ETL_WORKFLOW_SEQ")
      public class EtlWorkflowSeq {

           @Id
           @Column(name="WORKFLOW_SEQ_ID")
           public Integer id;
           
           @OneToOne(fetch=FetchType.EAGER)
           @JoinColumn(name="WORKFLOW_ID")
           public EtlWorkflow etlWorkflow;
           
           @Column(name="IS_NAME")
           public String integrationServiceName;
           
           @Column(name="INFO_SYSTEM_INST_CD")
           public String infoSystemInstCd;
           
           @Column(name="DISABLE")
           public Integer disable;
           
      }


      /* But the ETL_WORKFLOW_SEQ_REL blasted my brain :( */

      @Entity
      @Table(name="ETL_WORKFLOW_SEQ_REL")
      public class EtlWorkflowSeqNode {
           
           @EmbeddedId
           public EtlWorkflowSeqNodeId etlWorkflowSeqNodeId;

           @OneToMany(fetch=FetchType.LAZY)
           @JoinColumns(value={@JoinColumn(name="JOB_ID"),@JoinColumn(name="WORKFLOW_SEQ_ID")})
           public Set<EtlWorkflowSeqNode> children;

           @JoinColumns(value={@JoinColumn(name="JOB_ID"),@JoinColumn(name="REL_WORKFLOW_SEQ_ID")})
           @OneToMany(fetch = FetchType.EAGER)
           public Set<EtlWorkflowSeqNode> parents;     
           
           @Embeddable
           public static class EtlWorkflowSeqNodeId implements Serializable{          
                private static final long serialVersionUID = 1L;

                public EtlWorkflowSeqNodeId(){}
                
                @Column(name="JOB_ID")
                public Integer jobId;
           
                
                @Column(name="REL_WORKFLOW_SEQ_ID")
                public Integer parentId;
                          
                public String toString(){
                     return "EtlWorkflowSeqNodeId{jobId=>"+jobId+",parent=>"+parentId+"}";
                }                    
           }
           
      }


      And I'm getting stackoverflow when I'm trying to get all tree.