- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
调用saveAll
时我的方法JpaRepository
很长List<Entity>
从服务层来看,Hibernate 的跟踪日志显示每个实体发出的单个 SQL 语句。
我可以强制它进行批量插入(即多行)而不需要手动摆弄 EntityManger
、事务等,甚至是原始 SQL 语句字符串?
对于多行插入,我的意思不仅仅是从:
start transaction
INSERT INTO table VALUES (1, 2)
end transaction
start transaction
INSERT INTO table VALUES (3, 4)
end transaction
start transaction
INSERT INTO table VALUES (5, 6)
end transaction
到:
start transaction
INSERT INTO table VALUES (1, 2)
INSERT INTO table VALUES (3, 4)
INSERT INTO table VALUES (5, 6)
end transaction
但改为:
start transaction
INSERT INTO table VALUES (1, 2), (3, 4), (5, 6)
end transaction
在 PROD 中,我使用的是 CockroachDB,性能差异很大。
以下是重现问题的最小示例(为简单起见,H2)。
./src/main/kotlin/ThingService.kt
:
package things
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.data.jpa.repository.JpaRepository
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.GeneratedValue
interface ThingRepository : JpaRepository<Thing, Long> {
}
@RestController
class ThingController(private val repository: ThingRepository) {
@GetMapping("/test_trigger")
fun trigger() {
val things: MutableList<Thing> = mutableListOf()
for (i in 3000..3013) {
things.add(Thing(i))
}
repository.saveAll(things)
}
}
@Entity
data class Thing (
var value: Int,
@Id
@GeneratedValue
var id: Long = -1
)
@SpringBootApplication
class Application {
}
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
./src/main/resources/application.properties
:
jdbc.driverClassName = org.h2.Driver
jdbc.url = jdbc:h2:mem:db
jdbc.username = sa
jdbc.password = sa
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create
spring.jpa.generate-ddl = true
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.jdbc.batch_size = 10
spring.jpa.properties.hibernate.order_inserts = true
spring.jpa.properties.hibernate.order_updates = true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data = true
./build.gradle.kts
:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
val kotlinVersion = "1.2.30"
id("org.springframework.boot") version "2.0.2.RELEASE"
id("org.jetbrains.kotlin.jvm") version kotlinVersion
id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion
id("org.jetbrains.kotlin.plugin.jpa") version kotlinVersion
id("io.spring.dependency-management") version "1.0.5.RELEASE"
}
version = "1.0.0-SNAPSHOT"
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = listOf("-Xjsr305=strict")
}
}
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.jetbrains.kotlin:kotlin-reflect")
compile("org.hibernate:hibernate-core")
compile("com.h2database:h2")
}
运行:
./gradlew bootRun
触发数据库插入:
curl http://localhost:8080/test_trigger
日志输出:
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: select thing0_.id as id1_0_0_, thing0_.value as value2_0_0_ from thing thing0_ where thing0_.id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
Hibernate: insert into thing (value, id) values (?, ?)
最佳答案
要使用 Spring Boot 和 Spring Data JPA 进行批量插入,您只需要两件事:
将选项 spring.jpa.properties.hibernate.jdbc.batch_size
设置为您需要的适当值(例如:20)。
使用你的 repo 的 saveAll()
方法和准备插入的实体列表。
工作示例是 here .
关于插入语句的转换成这样的:
INSERT INTO table VALUES (1, 2), (3, 4), (5, 6)
PostgreSQL 中提供了这样的功能:您可以在 jdbc 连接字符串中将选项 reWriteBatchedInserts
设置为 true:
jdbc:postgresql://localhost:5432/db?reWriteBatchedInserts=true
然后jdbc驱动会做this transformation .
您可以找到有关批处理的其他信息 here .
更新
Kotlin 中的演示项目:sb-kotlin-batch-insert-demo
更新
关于hibernate - 如何使用 JpaRepository 进行批量(多行)插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50772230/
我的 spring 应用程序中有一个功能有问题。我在同一个数据库中有 2 个表,都包含相同类型的数据(ID、标题、描述和日期)。我可以从一张表中获取数据,但不知道如何插入到第二张表中。 在我的@Ser
我有如下 3 个实体类(示例取自 https://hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example
是否可以在没有实体的情况下使用 JpaRepository?在这种情况下,将其替换为 DTO。 如下示例 @Repository public interface BffRepository ext
我有使用 JPA 工具生成的用户实体和角色实体。 User 和 Role 是多对多的关系。我使用以下代码插入管理员,但是,没有数据插入到 user_role 表中。我还注意到角色类有 @JoinTab
我使用 JpaRepository 来保存数据,但 hibernate.show_sql 显示“选择”并且不会保存数据。以下是我的服务: @Autowired private UserReposito
我正在使用 Spring Boot、Hibernate 和 JPA 存储库来搜索数据。 我想通过登录用户的上下文来过滤搜索结果。例如。查找返回登录用户拥有的所有实体的方法?我有许多用于过滤的 JPA
如何从父进程中检索子进程? 假设我有父类和子类。我想从父方检索子项列表。 这是我的家长类。 +import ... @Entity public class Parent { @Id
我正在努力尝试创建一种方法来从数据库获取对象并加载依赖项。 在正常情况下,我永远不想加载它,所以我的两个对象看起来像这样: public class Training implements Seria
由于某些奇怪的原因,Jpa 生成的查询没有按预期生成。我使用 jpa 在我的存储库中编写了以下查询: 我的对象 @Id @GeneratedValue(strategy = Generatio
我正在尝试 Autowiring 服务中的存储库,但我不断收到以下错误 org.springframework.beans.factory.UnsatisfiedDependencyException
我是使用 JPA 的新手,我正在在线阅读教程,所有这些教程都从 JPARespository 扩展而来,如下所示 从此页面 https://www.callicoder.com/spring-boot
我正在制作一个应用程序,该应用程序保存用户配置文件和具有许多交易的钱包。 这是事务类: @Entity @Table(name = "transactions") public class Trans
我正在 IntelliJ 中使用 Spring Boot 我已经扩展了 JpaRepository 接口(interface),并创建了一个未在存储库变量中显示的查询到我的 Controller 中(
我只想返回特定的文件以进行服务。所以我在 JPARepository 界面中编写了以下代码: @Query(value="select r.id from Ride r") public List f
我正在使用 spring JpaRepository,并且希望使用 el 表达式提供具有通用派生 SQL 查询的通用接口(interface),如下所示: public interface BaseR
我正在尝试做一个非常简单的删除操作,但不知何故它不起作用,因为我将 DAO 更新为 JpaRepository。基本上是这样的: A a = aRepository.findOne(id); a.se
我像这样使用数据存储库: interface HerbadgeRepository extends JpaRepository { } 在甲骨文they say : To implement gene
我使用 Spring 与 Hibernate 和 JpaRepository 作为数据库存储库。 我有两个用于用户存储的类: @Entity public class User { @Id
我想要保护 JpaRepository,它将我的 OAuth 用户引用到授权角色 ROLE_ADMIN。现在我想知道我需要注释哪些方法。因此,当我想保护整个用户存储库时,登录不再起作用,因为它调用 f
我们正在使用 JpaRepository 连接 mysql。我们的存储库如下所示: public interface IRawEntityRepository extends JpaRepositor
我是一名优秀的程序员,十分优秀!