views:

232

answers:

1

I mapped two classes in a ManyToMany association with these annotations :

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class TechnicalItem extends GenericBusinessObject implements Resumable{

    @SequenceGenerator(name="TECHNICAL_ITEM_ID_GEN", sequenceName="TECHNICAL_ITEM_ID_SEQ")
    @Id
    @Column(name = "\"ID\"", nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TECHNICAL_ITEM_ID_GEN")
    private int id;


    @ManyToMany(mappedBy = "referencePerformanceItems", fetch=FetchType.LAZY) 
    private List<TestingRate> testingRates;
}

@Entity
@DiscriminatorValue("T")
public class TestingRate extends Rate {


    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="ecc.\"TESTING_RATE_TECHNICAL_ITEM\"",
        joinColumns = {@JoinColumn(name = "\"TESTING_RATE_ID\"")}, 
        inverseJoinColumns = {@JoinColumn(name = "\"TECHNICAL_ITEM_ID\"")})
    //@ManyToMany(mappedBy = "testingRates", fetch=FetchType.LAZY)
    private List<TechnicalItem> referencePerformanceItems;
}

The sql generated for the association table creation is :

create table ecc."TESTING_RATE_TECHNICAL_ITEM" (
    "TESTING_RATE_ID" int4 not null,
    "TECHNICAL_ITEM_ID" int4 not null
);
alter table ecc."TESTING_RATE_TECHNICAL_ITEM" 
    add constraint FKC5D64DF6A2FE2698 
    foreign key ("TESTING_RATE_ID") 
    references ecc."RATE";

There is no mention of the second foreign key "TECHNICAL_ITEM_ID" (the second part of the composite foreign key which should be in the association table). Is it a normal behaviour ? What should I do in the mapping if I want my 2 columns are 2 foreign keys referencing the primary keys of my 2 concerned tables.

I use a PostGreSQL database and Hibernate as JPA provider.

+1  A: 

Make sure you did not miss the second constraint. I have similar cases, Hibernate generates the DB schema fine

@ManyToMany(targetEntity = Two.class, cascade = {
    CascadeType.ALL
})
@JoinTable(name = "ONE_M2MJT_TWO", joinColumns = {
    @JoinColumn(name = "ONE_ID")
}, inverseJoinColumns = {
    @JoinColumn(name = "TWO_ID")
}, uniqueConstraints = {
    @UniqueConstraint(columnNames = {
        "ONE_ID",
        "TWO_ID"
    })
})
public List<Two> getManyToManyJoinTable() { ... }


create table ONE (MY_SUPER_ID bigint generated by default as identity (start with 1), VALUE_ varchar(255), primary key (MY_SUPER_ID))
create table ONE_M2MJT_TWO (ONE_ID bigint not null, TWO_ID bigint not null, unique (ONE_ID, TWO_ID))
create table TWO (MY_SUPER_ID bigint generated by default as identity (start with 1), VALUE_ varchar(255), primary key (MY_SUPER_ID))
alter table ONE_M2MJT_TWO add constraint FKA575066C950B44B foreign key (TWO_ID) references TWO
alter table ONE_M2MJT_TWO add constraint FKA575066C045060B foreign key (ONE_ID) references ONE
lexicore
Thanks lexicore, I didn't know the "uniqueConstraint" parameter.But I figured out my problem : I tried to include an abstract class "TechnicalItem" in the many to many relation rather than the "PerformanceItem" class which inherits from "TechnicalItem".Then even without the "uniqueconstraint" parameter, the 2 foreign keys are created.Thanks anyway,Julien
Julien