JPA:如何提高OneToMany关系的持久性性能 [英] JPA: How to improve performance in persistance of OneToMany relationships

查看:224
本文介绍了JPA:如何提高OneToMany关系的持久性性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级:

@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;
}

第二类:

@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);

一切都很好并且可以正常工作,但是当我执行SQL事件探查器时,我发现它执行了很多会影响性能的语句.

Everything is good and working, but when i execute the SQL Profiler i find that it executes many statements, which affect performance.

是否有更好的方法来改进我的代码,以减少hibernate执行的语句数量并提高性能?

Is there a better way to improve my code, in order to reduce number of statements executed by hibernate and improve performance ?

谢谢

推荐答案

在这种情况下,客户端和文件夹之间是否存在多对多关联或一对多关联?

Is there many-to-many association or one-to-many association between client and folder in that case?

如果这是一对多关联,建议您使用双向映射.因为在这种情况下您不需要第三张表.因此(简短地说),休眠将生成较少的查询,并且性能将提高.

If that is one-to-many association, I suggest you use bidirectional mapping. Because you don't need the third table for this situation. So (briefly), fewer queries will be generated by hibernate and the performance will increase.

@Entity
@Table(name="folder")
public class Folder {
    @Id
    private String reference;

    @OneToMany(mappedBy="folder", cascade = {CascadeType.ALL}, orphanRemoval = true)
    private List<Client> clients= new ArrayList<>();

    private Date createDate;

    //getters and setters
}


@Entity
@Table(name = "client")
public class Client implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "folder_id")
    private Folder folder;

    //getters and setters
}

请参阅有关@OneToMany关系的精彩文章: https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

See this awesome post about @OneToMany relationships: https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

但是,如果您的案例是多对多的,请参阅: https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/

But, if your case is many-to-many see: https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/

这篇关于JPA:如何提高OneToMany关系的持久性性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆