Using the hbm2java tool for reverse engineering of database tables, the following problem exists:
At ManyToMany relations with composite keys an inverseJoinColumn is missing. I think it seems to be a bug in the tool. I've tested it with the latest release 3.2.2.Beta1 and with the nigthly build HibernateTools-3.2.4.CR1-N200810300202
Wrong code:
Code:
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "ET_MANY_TO_MANY_COMP_MAPPING",
joinColumns = {
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP1_ID", nullable = false, updatable = false),
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP11_ID", nullable = false, updatable = false) },
inverseJoinColumns = {
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP2_ID", nullable = false, updatable = false) })
public Set<EtManyToManyComp2> getEtManyToManyComp2s() {
return this.etManyToManyComp2s;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "ET_MANY_TO_MANY_COMP_MAPPING",
joinColumns = {
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP2_ID", nullable = false, updatable = false),
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP22_ID", nullable = false, updatable = false) },
inverseJoinColumns = {
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP1_ID", nullable = false, updatable = false) })
public Set<EtManyToManyComp1> getEtManyToManyComp1s() {
return this.etManyToManyComp1s;
}
Correct code:
Code:
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "ET_MANY_TO_MANY_COMP_MAPPING",
joinColumns = {
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP1_ID", nullable = false, updatable = false),
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP11_ID", nullable = false, updatable = false) },
inverseJoinColumns = {
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP2_ID", nullable = false, updatable = false),
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP22_ID", nullable = false, updatable = false) })
public Set<EtManyToManyComp2> getEtManyToManyComp2s() {
return this.etManyToManyComp2s;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "ET_MANY_TO_MANY_COMP_MAPPING",
joinColumns = {
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP2_ID", nullable = false, updatable = false),
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP22_ID", nullable = false, updatable = false) },
inverseJoinColumns = {
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP1_ID", nullable = false, updatable = false),
@JoinColumn(name = "FK_ET_MANY_TO_MANY_COMP1_ID", nullable = false, updatable = false) })
public Set<EtManyToManyComp1> getEtManyToManyComp1s() {
return this.etManyToManyComp1s;
}
For reproducing this, you can use the following Oracle SQL scripts:
Code:
CREATE TABLE ET_MANY_TO_MANY_COMP1 (
ET_MANY_TO_MANY_COMP1_ID NUMBER,
ET_MANY_TO_MANY_COMP11_ID NUMBER,
FIELD VARCHAR2(10),
PRIMARY KEY (ET_MANY_TO_MANY_COMP1_ID, ET_MANY_TO_MANY_COMP11_ID)
);
CREATE TABLE ET_MANY_TO_MANY_COMP2 (
ET_MANY_TO_MANY_COMP2_ID NUMBER,
ET_MANY_TO_MANY_COMP22_ID NUMBER,
FIELD VARCHAR2(10),
PRIMARY KEY (ET_MANY_TO_MANY_COMP2_ID, ET_MANY_TO_MANY_COMP22_ID)
);
CREATE TABLE ET_MANY_TO_MANY_COMP_MAPPING (
FK_ET_MANY_TO_MANY_COMP1_ID NUMBER,
FK_ET_MANY_TO_MANY_COMP11_ID NUMBER,
FK_ET_MANY_TO_MANY_COMP2_ID NUMBER,
FK_ET_MANY_TO_MANY_COMP22_ID NUMBER,
FOREIGN KEY (FK_ET_MANY_TO_MANY_COMP1_ID,FK_ET_MANY_TO_MANY_COMP11_ID)
REFERENCES ET_MANY_TO_MANY_COMP1(ET_MANY_TO_MANY_COMP1_ID,ET_MANY_TO_MANY_COMP11_ID),
FOREIGN KEY (FK_ET_MANY_TO_MANY_COMP2_ID, FK_ET_MANY_TO_MANY_COMP22_ID)
REFERENCES ET_MANY_TO_MANY_COMP2(ET_MANY_TO_MANY_COMP2_ID, ET_MANY_TO_MANY_COMP22_ID),
PRIMARY KEY (FK_ET_MANY_TO_MANY_COMP1_ID,FK_ET_MANY_TO_MANY_COMP11_ID,FK_ET_MANY_TO_MANY_COMP2_ID,FK_ET_MANY_TO_MANY_COMP22_ID)
);