-->
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: Self referencing table via Join Table
PostPosted: Mon Feb 25, 2008 11:14 am 
Newbie

Joined: Mon Feb 25, 2008 10:26 am
Posts: 2
Dear fellow developers,
I have been searching through this and other sites on the web for a solution to my problem but have had no joy as yet. Here goes....

The database schema I'm using has the following table structures defined
HL7_Structures table
Code:
 
   CREATE TABLE “HL7_STRUCTURES"
   (   "SCHEME_NAME" VARCHAR2(50 BYTE),
   "VERSION" VARCHAR2(50 BYTE),
   "STRUCTURE_CODE" VARCHAR2(50 BYTE),
   "NAME" VARCHAR2(255 BYTE),
   "DESCRIPTION" VARCHAR2(50 BYTE),
   "ISGROUP" NUMBER(1,0),
   "REPEATING" NUMBER(1,0),
   "VERSIONING" NUMBER(18,0),
    CONSTRAINT "HL7STRUCTURE_PK" PRIMARY KEY ("SCHEME_NAME", "VERSION", "STRUCTURE_CODE"),
    CONSTRAINT "HL7STRUCTURE_FK" FOREIGN KEY ("SCHEME_NAME", "VERSION")
     REFERENCES "SCHEMES" ("SCHEME_NAME", "VERSION")
   )


HL7_CHILD_STRUCTURES table
Code:
CREATE TABLE "HL7_CHILD_STRUCTURES"
   (   "SCHEME_NAME" VARCHAR2(50 BYTE),
   "VERSION" VARCHAR2(50 BYTE),
   "PARENT_STRUCTURE_CODE" VARCHAR2(50 BYTE),
   "CHILD_STRUCTURE_CODE" VARCHAR2(50 BYTE),
   "SORT_ORDER" VARCHAR2(5 BYTE),
   "VERSIONING" NUMBER(18,0),
    CONSTRAINT "HL7CHILDSTRUCTURE_PK" PRIMARY KEY ("SCHEME_NAME", "VERSION", "PARENT_STRUCTURE_CODE", "CHILD_STRUCTURE_CODE"),
    CONSTRAINT "HL7CHILDSTRUCTURESFK1" FOREIGN KEY ("SCHEME_NAME", "VERSION", "PARENT_STRUCTURE_CODE")
     REFERENCES "HL7_STRUCTURES" ("SCHEME_NAME", "VERSION", "STRUCTURE_CODE") ON DELETE CASCADE ENABLE,
    CONSTRAINT "HL7CHILDSTRUCTURESFK2" FOREIGN KEY ("SCHEME_NAME", "VERSION", "CHILD_STRUCTURE_CODE")
     REFERENCES "HL7_STRUCTURES" ("SCHEME_NAME", "VERSION", "STRUCTURE_CODE") ENABLE
   )


Due to the data that is being stored in these tables you end up with a parent having MANY children. And a child is able to be related to MANY parents. I do understand this is a very strange data model, but due to the nature of the data it is what is needed.

When Entity classes are generated from the database, the HL7_STRUCTURES table is generated as a POJO but the HL7_CHILD_STRUCTURES table is a join table.

HL7_STRUCTURES POJO as generated
Code:
@Entity
@Table(name = "HL7_STRUCTURES")
@NamedQueries({@NamedQuery(name = "Structure.findBySchemeName", query = "SELECT s FROM Structure s WHERE s.structurePK.schemeName = :schemeName"), @NamedQuery(name = "Structure.findByVersion", query = "SELECT s FROM Structure s WHERE s.structurePK.version = :version"), @NamedQuery(name = "Structure.findByStructureCode", query = "SELECT s FROM Structure s WHERE s.structurePK.structureCode = :structureCode"), @NamedQuery(name = "Structure.findByName", query = "SELECT s FROM Structure s WHERE s.name = :name"), @NamedQuery(name = "Structure.findByDescription", query = "SELECT s FROM Structure s WHERE s.description = :description"), @NamedQuery(name = "Structure.findByIsgroup", query = "SELECT s FROM Structure s WHERE s.isgroup = :isgroup"), @NamedQuery(name = "Structure.findByRepeating", query = "SELECT s FROM Structure s WHERE s.repeating = :repeating"), @NamedQuery(name = "Structure.findByVersioning", query = "SELECT s FROM Structure s WHERE s.versioning = :versioning")})
public class Structure implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected StructurePK structurePK;
    @Column(name = "NAME")
    private String name;
    @Column(name = "DESCRIPTION")
    private String description;
    @Column(name = "ISGROUP")
    private Short isgroup;
    @Column(name = "REPEATING")
    private Short repeating;
    @Column(name = "VERSIONING")
    private Long versioning;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "structure")
    private Collection<DataField> dataFieldCollection;   
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "structure")
    private Collection<EventStructure> eventStructureCollection;   
    @JoinColumns({@JoinColumn(name = "SCHEME_NAME", referencedColumnName = "SCHEME_NAME", insertable = false, updatable = false), @JoinColumn(name = "VERSION", referencedColumnName = "VERSION", insertable = false, updatable = false)})
    @ManyToOne
    private Scheme scheme;

    @JoinTable(name = "HL7_CHILD_STRUCTURES", joinColumns = {@JoinColumn(name = "SCHEME_NAME", referencedColumnName = "SCHEME_NAME"), @JoinColumn(name = "VERSION", referencedColumnName = "VERSION"), @JoinColumn(name = "PARENT_STRUCTURE_CODE", referencedColumnName = "STRUCTURE_CODE")}, inverseJoinColumns = {@JoinColumn(name = "SCHEME_NAME", referencedColumnName = "SCHEME_NAME"), @JoinColumn(name = "VERSION", referencedColumnName = "VERSION"), @JoinColumn(name = "CHILD_STRUCTURE_CODE", referencedColumnName = "STRUCTURE_CODE")})
    @ManyToMany
    private Collection<Structure> structureCollection;   
    @ManyToMany(mappedBy = "structureCollection")   
    private Collection<Structure> structureCollection;

//class definiton continues...


When run I first get an error that structureCollection has already defined. Which I can see as being a variable with structureCollection having been defined twice.

Once this has been changed to avoid the naming conflict, which I run the code the following error appears


Code:
25-Feb-2008 14:57:51 org.jdesktop.application.Application$1 run
SEVERE: Application class messageViewer.appMain failed to launch
javax.persistence.PersistenceException: org.hibernate.MappingException: Repeated column in mapping for collection: model.lookup.Structure.structureCollection column: SCHEME_NAME
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:737)
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
        at utility.JPAUtil.getEntityManagerFactory(JPAUtil.java:42)
        at messageViewer.messageViewPanel.<init>(messageViewPanel.java:45)
        at messageViewer.messageViewPanel.getInstance(messageViewPanel.java:58)
        at messageViewer.appMainView.<init>(appMainView.java:103)
        at messageViewer.appMain.startup(appMain.java:63)
        at org.jdesktop.application.Application$1.run(Application.java:171)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: model.lookup.Structure.structureCollection column: SCHEME_NAME
        at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:306)
        at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:329)
        at org.hibernate.mapping.Collection.validate(Collection.java:286)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1106)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:730)
        ... 16 more
Exception in thread "AWT-EventQueue-0" java.lang.Error: Application class messageViewer.appMain failed to launch
        at org.jdesktop.application.Application$1.run(Application.java:177)


I may be wrong but from what I can understand since the problem lies in the fact that it is in effect the same table (HL7_STRUCTURES) either side of the join table. I am using Annotations but from what I have read I may have more luck if trying to perform this mapping using HBM.XML markup?

I hope you can shed any light on my problem.
Thanking you in advance,

Hiz


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.