I have two classes :
@Entity
@Table(name="folder")
public class Folder{
@Id
public String reference;
@OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true)
public List<Client> clients= new ArrayList<>();
public Date createDate;
}
and the second class:
@Entity
@Table(name = "client")
public class Client implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int id;
public String name;
}
in my database i created an intermadiate table
create table folder_clients (folder_ref varchar(20) not null, clients_id int not null)
alter table folder_clients add constraint UK_clientid unique (clients_id)
alter table folder_clients add constraint FK_idClient foreign key (clients_id) references client
alter table folder_clients add constraint FK_refFolder foreign key (folder_ref) references folder
now i have a service that persists a folder, so automatically it persists all clients related to it, and this is done throught a Folder Repository:
folder.getClients().add(client);
folderRepository.save(folder);
Everything is good and working, but when i execute the SQL Profiler i find that it executes many statements, which affect performance.
Is there a better way to improve my code, in order to reduce number of statements executed by hibernate and improve performance ?
thank you