Declare JPA entity for a tree element -
i have db table unidirectional trees. leafs of these trees can have several children/parents. cycles restricted. here db table definition:
create multiset table workflow_seq_rel ,no fallback , no before journal, no after journal, checksum = default, default mergeblockratio ( workflow_seq_id integer not null, rel_workflow_seq_id integer not null, job_id bigint) primary index ( workflow_seq_id ); as can see doesn't have primary key right now. appear later :) pk is: job_id+parent_id+child_id.
the idea is:
- rel_workflow_seq_id = parent
- workflow_seq_id = child
- job_id = tree_identificator (a determinant separate different trees stored in 1 table).
i'm trying declare jpa entity:
@entity @table(name="workflow_seq_rel") public class etlworkflowseqnode { @embeddedid public etlworkflowseqnodeid etlworkflowseqnodeid; //@column(name="job_id") //public integer jobid; @embeddable class etlworkflowseqnodeid{ @column(name="job_id") public integer jobid; @manytoone(fetch = fetchtype.eager) @joincolumn(name="rel_workflow_seq_id") //etlworkflowseq.id = pk of etlworkflowseq entity public etlworkflowseq parent; @onetomany(fetch=fetchtype.eager /*, mappedby="parent"*/) @joincolumn(name="workflow_seq_id") public set<etlworkflowseq> children; } } and gen error: caused by: org.hibernate.annotationexception: foreign key refering models.etlworkflowseqnode models.etlworkflowseq has wrong number of column. should 2
here etlworkflowseq entity:
@entity @table(name="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; } what do wrong?
upd: here table definitions: --a bad design. pk should be: workflow_seq_id + rel_workflow_seq_id + job_id
create multiset table workflow_seq_rel ,no fallback , no before journal, no after journal, checksum = default, default mergeblockratio ( workflow_seq_id integer not null, --a ref child rel_workflow_seq_id integer not null, -- ref parent start_type_id smallint not null, -- type of connection disable_start_type_id smallint, -- other type of connection job_id bigint) -- tree determinant, primary index ( workflow_seq_id ); create multiset table workflow_seq ,no fallback , no before journal, no after journal, checksum = default, default mergeblockratio ( workflow_seq_id integer not null, -- id workflow_id bigint not null, -- ref original workflow, not interesting is_name varchar(255) character set unicode not casespecific, -- name info_system_inst_cd varchar(255) character set unicode not casespecific, -- other name disable byteint) -- garbage unique primary index ( workflow_seq_id ); -- should pk the idea several trees stored in workflow_seq_rel job_id determinant trees. workflow_seq_id, rel_workflow_seq_id refer cutomized template rel_workflow_seq table.
i cannot noticing there inconsistency in question.
you first state that:
leafs of these trees can have several children/parents.
this in believe makes relationship between leafs many many.
as make of question etlworkflowseq represent leafs, think etlworkflowseqnode represents relationship between etlworkflowseq objects?
however, nodes point one parent , many children.
you can use create similar:
@entity @table(name="workflow_seq") public class etlworkflowseq { @id @generatedvalue @column(name="workflow_seq_id") public integer id; @manytoone @joincolumn(name="workflow_id") public etlworkflow etlworkflow; @manytomany @jointable(name = "workflow_seq_rel") private set<etlworkflowseq> children; @manytomany(mappedby = "children") private set<etlworkflowseq> parents; @manytoone @joincolumn(name = "job_id", referencedcolumnname = "id") private job job; } this make etlworkflowseqnode , etlworkflowseqnodeid obsolete.
i state when using @embeddable should use base types in them. using other base types not possible/causes problems/is not standard (correct me if i'm wrong).
if use foreign keys in composite primary key can use this:
@entity public class foo { @id private long id; } @entity public class bar { @embeddedid private barpk key; @mapsid(value = "fooid") @manytoone @joincolumns({ @joincolumn(name = "foo_id", referencedcolumnname = "id") }) private foo foo; } @embeddable public class barpk { @column(name = "id") private long id; @column(name = "foo_id") private long fooid; }
Comments
Post a Comment