我尝试使用JPA / Hibernate / Postgres将数据插入Postgres。 数据从CSV文件解析,然后应保存到postgres数据库中。保留数据的代码如下所示:
@Autowired
KundeRepository repo;
@Transactional
public void safe(Kunde kd) {
repo.save(kd);
}
public void safeAll(Iterable<Kunde> kt) {
repo.save(kt);
repo.flush();
}
该实体如下所示
public class account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
}
@ManyToOne
Kunde kunde;
@OneToMany
List<TransaktionsGruppe> gruppen;
@Entity
@Table(name = "kunde")
@NoArgsConstructor
public class Kunde implements Serializable {
public static final String kundennummerKey = "KUNDENNUMMER";
private static final long serialVersionUID = 1L;
@Id
@Getter
@Setter
private String id;
@OneToMany
List<Account> accounts;
@Entity
@Table(name = "transaktionsgruppe")
public class Transaktionsgruppe implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
Account acc;
private String bezeichnung;
当我现在将Collection传递给safeAll方法时,插入操作非常慢。特别是它似乎为每个插入调用hibernate序列。有没有办法加快速度?
我的配置如下:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: dburl
username: user
password: pw
hikari:
validation-timeout: 10000
health-check-properties: {"connectivityCheckTimeoutMs","1000"}
jpa:
show-sql: true
properties:
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
hibernate.jdbc.batch.size: 100
hibernate.order_inserts: true+
目前show-sql已启用。它总共约有60000个实体,需要超过20分钟。权利的规模相当小
答案 0 :(得分:0)
您的safeAll()
方法未使用@Transactional
进行注释,因此Spring会打开并关闭列表中每个项目的事务。通过对其进行注释,Spring将打开并关闭整个列表中的单个事务。