Hi this is hard to explain since I don’t speak english so… This is my code
@Entity
@Table(name = "table_a")
public class TableA implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@Basic(optional = false)
@Column(name = "name")
private String name;
}
@Entity
@Table(name = "table_b")
public class TableB implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@Basic(optional = false)
@Column(name = "name")
private String name;
}
public class TableAb implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private TableAbPK tableAbPK;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@MapsId("idA")
@JoinColumn(name = "id_a",unique = false,insertable = false, updatable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private TableA tableA;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@MapsId("idB")
@JoinColumn(name = "id_b",unique = false,insertable = false, updatable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private TableB tableB;
}
@Embeddable
public class TableAbPK implements Serializable {
//@Basic(optional = false)
@Column(name = "id_a")
private long idA;
//@Basic(optional = false)
@Column(name = "id_b")
private long idB;
}
@Entity
@Table(name = "table_da")
public class TableDa implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private TableDaPK tableDaPK;
@ManyToOne(optional = false)
@JoinColumns({
@JoinColumn(name = "id_a", referencedColumnName = "id_a") //,insertable = false, updatable = false, unique=false
//@JoinColumn(name = "id_b", referencedColumnName = "id_b")
})
private TableAb tableAb;
@ManyToOne(optional = false)
@MapsId("idD")
@JoinColumn(name = "id_d")
private TableD tableD;
}
@Entity
@Table(name = "table_d")
public class TableD implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@Basic(optional = false)
@Column(name = "name")
private String name;
}
Everything runs “good”, but the problem is here…
Hibernate: create table table_a (id bigint not null auto_increment, name varchar(255) not null, primary key (id)) engine=InnoDB
Hibernate: create table table_ab (id_a bigint not null, id_b bigint not null, primary key (id_a, id_b)) engine=InnoDB
Hibernate: create table table_b (id bigint not null auto_increment, name varchar(255) not null, primary key (id)) engine=InnoDB
Hibernate: create table table_c (id bigint not null auto_increment, name varchar(255) not null, id_a bigint not null, id_b bigint not null, primary key (id)) engine=InnoDB
Hibernate: create table table_d (id bigint not null auto_increment, name varchar(255) not null, primary key (id)) engine=InnoDB
Hibernate: create table table_da (ida bigint not null, id_d bigint not null, id_a bigint not null, primary key (ida, id_d)) engine=InnoDB
Hibernate: alter table table_ab add constraint UK_djv0nep3kck06gw36xd4tlt3m unique (id_a)
Hibernate: alter table table_c add constraint UKsiky7nbvaossuaai9jjpaxv7v unique (id_a, id_b)
Hibernate: alter table table_ab add constraint FKhr6nmhfq7iugl09cpvk70k33e foreign key (id_a) references table_a (id) on delete cascade
Hibernate: alter table table_ab add constraint FKcp354skj76oh5vw26lyrws45c foreign key (id_b) references table_b (id) on delete cascade
Hibernate: alter table table_c add constraint FKg9vjngl4y25kl2ij6apaedqk2 foreign key (id_a, id_b) references table_ab (id_a, id_b)
Hibernate: alter table table_da add constraint FK52svk9pn8pqcoywlwsjflphkg foreign key (id_a) references table_ab (id_a)
Hibernate: alter table table_da add constraint FK78v3oxk0bagsws5hiv0x38w4c foreign key (id_d) references table_d (id)
The problem is
Hibernate: alter table table_ab add constraint UK_djv0nep3kck06gw36xd4tlt3m unique (id_a)
HIbernate is creating an unique column on Ab Table, so I won’t be able to add something like this:
(1,1) and (1,2), because id_a column is unique and I’ll be getting an exception. How can I fix this issue? the behavior changes to the desired one when I delete the table_da class… see this:
Hibernate: create table table_a (id bigint not null auto_increment, name varchar(255) not null, primary key (id)) engine=InnoDB
Hibernate: create table table_ab (id_a bigint not null, id_b bigint not null, primary key (id_a, id_b)) engine=InnoDB
Hibernate: create table table_b (id bigint not null auto_increment, name varchar(255) not null, primary key (id)) engine=InnoDB
Hibernate: create table table_c (id bigint not null auto_increment, name varchar(255) not null, id_a bigint not null, id_b bigint not null, primary key (id)) engine=InnoDB
Hibernate: create table table_d (id bigint not null auto_increment, name varchar(255) not null, primary key (id)) engine=InnoDB
Hibernate: alter table table_c add constraint UKsiky7nbvaossuaai9jjpaxv7v unique (id_a, id_b)
Hibernate: alter table table_ab add constraint FKhr6nmhfq7iugl09cpvk70k33e foreign key (id_a) references table_a (id) on delete cascade
Hibernate: alter table table_ab add constraint FKcp354skj76oh5vw26lyrws45c foreign key (id_b) references table_b (id) on delete cascade
Hibernate: alter table table_c add constraint FKg9vjngl4y25kl2ij6apaedqk2 foreign key (id_a, id_b) references table_ab (id_a, id_b)
As you can see everything is great and is working as expected. I’ve tried many options, but looks like is not possible to get this working using the code posted previously.