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.