mysql - Spring DAO rewritebatchedstatements 不适用于具有 embeddedId 的实体上的 SaveAll()
问题描述
我们需要在mysql上快速插入 500 万条记录(对此实例没有控制)。
尝试了以下所有方法,但没有任何效果:
- 设置
rewritebatchedstatements=true
(通过 url 和 props) - 试过
useServerPrepStmts=true
了useServerPrepStmts=true
- 尝试了以下其他休眠道具
Story
有所以GenerationType.Auto
没关系,但是批处理应该有效吗?(因为没有指定代)Task
EmbeddedId
- 试过
MySql5InnoDBDialect
了MySql5DBDialect
批处理工作(基于统计数据)但rewritebatchedstatements
不工作。
spring.jpa.properties.hibernate.jdbc.batch_size=500
spring.jpa.properties.hibernate.jdbc.fetch_size=500
spring.jpa.properties.hibernate.jdbc.order_inserts=500
spring.jpa.properties.hibernate.jdbc.order_updates=500
spring.jpa.properties.hibernate.batch_versioned_data=true
spring.jpa.properties.hibernate.generate_statistics=true
spring.datasource.hikari.data-source-properties.rewriteBatchedStatements=true
#spring.datasource.hikari.data-source-properties.useServerPrepStmts=true
spring.datasource.hikari.data-source-properties.useServerPrepStmts=false
示例实体:
class SprintService {
@Transactional
public void startSprint(Epic epic, List<Stories> stories) {
epic.save()
.....
stories.saveAll() // few hundreds of records
Set<Task> tasks = new HashSet<>;
for(Story story: stories)
{
tasks.addAll(story.getTasks());
tasks.addAll(story.getTasks());
tasks.addAll(story.getTasks());
//do something
}
tasks.saveAll() // few million records
}
}
@Table(name = "STORY")
@EqualsAndHashCode
class Story {
@Id
@GeneratedValue(stratergy=GenerationType.Auto)
@Column(name="STORY_ID")
Long id;
@Column(name="STORY_NAME")
String name;
@OneToMany(cascade=MERGE, mappedBy="story", fetch = FetchType.Lazy)
Set<Task> task;
}
@Entity
@Table(name = "TASK_XREF")
class Task {
@EmbeddedId
TaskPKId taskPKId;
@Column(name = "TASK_NAME")
String name;
@MapsId("storyId")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "STORY_ID")
Story story;
//getters, setters
}
这是 TaskPKID 类:
@Embeddable
class TaskPKId implements Serializable {
long taskId;
long taskTypeId;
@Column(name="STORY_ID")
long storyId;
public long getTaskId() {
return taskId;
}
public void setTaskId(long taskId) {
this.taskId = taskId;
}
public void setTaskTypeId(long taskTypeId) {
this.taskTypeId = taskTypeId;
}
}
解决方案
这些值应该是布尔值。
spring.jpa.properties.hibernate.jdbc.order_inserts=true
spring.jpa.properties.hibernate.jdbc.order_updates=true
推荐阅读
- java - 在 try-catch 块中,“finally”是否会覆盖异常块中的“return”?
- python - 用于在 Python 中提取脚本标签的正则表达式
- python - 如何在抽象模型的 ManyToMany 字段中设置related_name?
- sql - 更改后如何知道列的先前长度
- laravel - Laravel 5 从其他表中获取关系数据而不使用数据透视表
- javascript - 在同一个数组元素上使用 $regex 和 $nor 运算符
- mysql - 如何使用 select sum() 按组选择值?
- java - 将 SpringDataJpa 与 Hibernate/eclipselink 一起使用时 entityManager 的行为
- javascript - 无法弄清楚如何通过调整窗口大小使 toggleClass 正常工作
- swift - NSLayoutAnchor 的类型