-->
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: Columns reuse in relationship cause Repeated column error
PostPosted: Tue Jun 15, 2010 10:45 am 
Newbie

Joined: Mon May 31, 2010 7:29 am
Posts: 5
I am facing a complicated problem on entity mapping for a legacy database. The applicaton i am working uses a unnomalization pattern that reuses columns from the PK to the FKs.

The application is projected to many structures (use the same base to many companies), so every table has the column ID_STRUCTURE. The problem is that when a FK is made the ID_STRUCTURE from the origin table is not trasported to the destination table. Like this:

Code:

CREATE TABLE FSWCT_CAMPAIGN
(
   ID_STRUCTURE   NUMBER(8,2) NOT NULL,
   ID_CAMPAIGN    NUMBER(8,2) NOT NULL,
   ID_PRODUCT     NUMBER(8,2),
   NAME_CAMPAIGN  VARCHAR2(50)
)
;

CREATE TABLE FSWCT_PRODUCT
(
   ID_STRUCTURE  NUMBER(8,2) NOT NULL,
   ID_PRODUCT    NUMBER(8,2) NOT NULL,
   DESC_PRODUCT  VARCHAR2(50)
)
;

ALTER TABLE FSWCT_CAMPAIGN ADD CONSTRAINT PK_FWSCT_COMPANY
   PRIMARY KEY (ID_STRUCTURE, ID_CAMPAIGN)
;

ALTER TABLE FSWCT_PRODUCT ADD CONSTRAINT PK_FSWCT_PRODUCT
   PRIMARY KEY (ID_STRUCTURE, ID_PRODUCT)
;

ALTER TABLE FSWCT_CAMPAIGN ADD CONSTRAINT FK_FWSCT_COMPANY_FSWCT_PRODUCT
   FOREIGN KEY (ID_STRUCTURE, ID_PRODUCT) REFERENCES FSWCT_PRODUCT (ID_STRUCTURE, ID_PRODUCT)
;



Note that ID_STRUCTURE from PRODUCT is referenced by ID_STRUCTURE (part of CAMPAIGN PK). The common way to this relationship would be create a PRODUCT_ID_STRUCTURE in CAMPAIGN to reference ID_STRUCTURE from PRODUCT, but in this case I cant change the database model.

My mapping is:
Code:
@Entity
@Table(name = "FSWCT_CAMPAIGN")
public class Campaign implements java.io.Serializable {
   
   @EmbeddedId
   @AttributeOverrides( {
         @AttributeOverride(name = "idStructure", column = @Column(name = "ID_STRUCTURE", nullable = false, precision = 8)),
         @AttributeOverride(name = "idCampaign", column = @Column(name = "ID_CAMPAIGN", nullable = false, precision = 8)) })
   private CampaignId id;
   
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumns( {
         @JoinColumn(name = "ID_STRUCTURE", referencedColumnName = "ID_STRUCTURE", nullable = false),
         @JoinColumn(name = "ID_PRODUCT", referencedColumnName = "ID_PRODUCT", nullable = false) })
   private Product product;
   
   @Column(name = "NAME_CAMPAIGN", length = 50)
   private String nameCampaign;
}

Code:
@Embeddable
public class CampaignId implements java.io.Serializable {
   
   @Column(name = "ID_STRUCTURE", nullable = false, precision = 8)
   private Long idStructure;
   
   @Column(name = "ID_CAMPAIGN", nullable = false, precision = 8)
   private Long idCampaign;
}

Code:
@Entity
@Table(name = "FSWCT_PRODUCT", schema = "DESE10G")
public class Product implements java.io.Serializable {
   
   @EmbeddedId
   @AttributeOverrides( {
         @AttributeOverride(name = "idStructure", column = @Column(name = "ID_STRUCTURE", nullable = false, precision = 8)),
         @AttributeOverride(name = "idProduct", column = @Column(name = "ID_PRODUCT", nullable = false, precision = 8)) })
   private ProductId id;
   
   @Column(name = "DESC_PRODUCT", length = 50)
   private String descProduct;
}

Code:
@Embeddable
public class ProductId implements java.io.Serializable {
   
   @Column(name = "ID_STRUCTURE", nullable = false, precision = 8)
   private Long idStructure;
   
   @Column(name = "ID_PRODUCT", nullable = false, precision = 8)
   private Long idProduct;
}


When a try to insert a new Campaign this error is thrown:

Code:
org.hibernate.MappingException: Repeated column in mapping for entity: model.Campaign column: ID_STRUCTURE (should be mapped with insert="false" update="false")


I couldnt find help anywhere because this database modeling strategy is rare.
Is there some way to map this kind of relationship? Any help is apreciated.


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.