gpt4 book ai didi

java - Hibernate:批量和非批量插入生成相同数量的 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 10:34:22 24 4
gpt4 key购买 nike

有一个代码示例,由两个具有双向映射的实体组成。

@Entity
@Table(name = "Post")
@NoArgsConstructor
@Data
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Comment> comments = new HashSet<>();

public void addComment(Comment comment) {
comments.add(comment);
}

public void removeComment(Comment comment) {
comments.remove(comment);
}
}

@Entity
@Table(name = "Comment")
@NoArgsConstructor
@Data
@EqualsAndHashCode(exclude = {"id", "post"})
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(nullable = false)
private String review;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", updatable = false, nullable = false)
private Post post;

public Comment(Post post, String review) {
this.post = post;
this.review = review;
}
}

配置

@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@PropertySource("classpath:application.properties")
@ComponentScan("jpaDifferent")
public class RootConfig {

public static void main(String[] args) throws Exception {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RootConfig.class);
DaoCollections daoCollections = applicationContext.getBean(DaoCollections.class);
Post post = new Post();
for (int i = 0; i < 30; i++) {
post.addComment(new Comment(post, "Comment No. " + i));
}
daoCollections.savePost(post);
}

@Bean
@ConfigurationProperties(prefix = "db")
public DataSource dataSource() {
return new BasicDataSource();
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
sfb.setDataSource(dataSource());
sfb.setPackagesToScan("jpaDifferent");
sfb.setHibernateProperties(properties());
return sfb;
}

private Properties properties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.jdbc.batch_size", "10");
properties.setProperty("hibernate.order_inserts", "true");
properties.setProperty("hibernate.order_updates", "true");
properties.setProperty("hibernate.jdbc.batch_versioned_data", "true");
return properties;
}

@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
}

@Repository
@Transactional
public class DaoCollections {

@Autowired
private SessionFactory sessionFactory;

private Session currentSession() {
return sessionFactory.getCurrentSession();
}

public void savePost(Post post) {
currentSession().persist(post);
}
}

Hibernate 日志包含 30 行“插入”:

Hibernate: insert into Post values ( )
Hibernate: insert into Comment (post_id, review) values (?, ?)
Hibernate: insert into Comment (post_id, review) values (?, ?)
...........
Hibernate: insert into Comment (post_id, review) values (?, ?)

根据包含有关批处理的说明的配置文件中的属性,我预计会生成 3 个“插入”SQL 查询。但实际上有 30 个,每个“评论”对应一个。我不知道我是否误解了有关批处理的内容,事实上,它不应该优化查询数量,或者可能只是配置有问题。

最佳答案

这是因为身份ID。当 Id 为 Identity 类型时,Hibernate 不会进行批处理。使用自定义或序列。

关于java - Hibernate:批量和非批量插入生成相同数量的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com