Hibernate JPA为oneToMany实体生成大量的sequence.nextval查询

问题描述 投票:0回答:1

我有一个父表和一个子表。父级可能包含许多子条目。每当我进行父保存时,它会为每个子条目的序列生成生成许多select语句。 select ITEM_SEQ,nextval from dual

有没有办法避免这种情况。我正在寻找一个直接的声明,通过提高性能不会在那里产生这个nextval代。

@Entity
@Table(name="Bucket")
public class Bucket {
Long id;

@OneToMany(mappedBy = "bucket", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<Item> items = new TreeSet<>();
.....
....
}



@Entity
@Table(name = "Item")
public class Item {



    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ItemGenerator")
    @SequenceGenerator(name = "ItemGenerator", sequenceName = "ITEM_SEQ", allocationSize = 1)
    @Column(name = "ITEM_SEQ")
    private Long id;
...
...
}
hibernate spring-boot jpa
1个回答
1
投票

GenerationType.SEQUENCE需要额外调用以从DB序列中获取新ID。在大多数应用程序中,这不是性能问题。如果您不希望它发生,请使用GenerationType.IDENTITY代替。

© www.soinside.com 2019 - 2024. All rights reserved.