JPA : many-to-many - only one foreign key in the association table
Posted
by Julien
on Stack Overflow
See other posts from Stack Overflow
or by Julien
Published on 2010-04-14T08:29:06Z
Indexed on
2010/04/14
8:33 UTC
Read the original article
Hit count: 573
Hi,
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 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 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.
Thanks, Julien
© Stack Overflow or respective owner