name: jpa-patterns description: 用于Spring Boot中实体设计、关系、查询优化、事务、审计、索引、分页和连接池的JPA/Hibernate模式。
JPA/Hibernate 模式
在Spring Boot中用于数据建模、仓库和性能调优。
实体设计
@Entity
@Table(name = "markets", indexes = {
@Index(name = "idx_markets_slug", columnList = "slug", unique = true)
})
@EntityListeners(AuditingEntityListener.class)
public class MarketEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 200)
private String name;
@Column(nullable = false, unique = true, length = 120)
private String slug;
@Enumerated(EnumType.STRING)
private MarketStatus status = MarketStatus.ACTIVE;
@CreatedDate private Instant createdAt;
@LastModifiedDate private Instant updatedAt;
}
启用审计:
@Configuration
@EnableJpaAuditing
class JpaConfig {}
关系与N+1问题预防
@OneToMany(mappedBy = "market", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PositionEntity> positions = new ArrayList<>();
- 默认使用延迟加载。在查询中根据需要可使用
JOIN FETCH - 避免在集合上使用
EAGER,读取路径使用DTO投影
@Query("select m from MarketEntity m left join fetch m.positions where m.id = :id")
Optional<MarketEntity> findWithPositions(@Param("id") Long id);
仓库模式
public interface MarketRepository extends JpaRepository<MarketEntity, Long> {
Optional<MarketEntity> findBySlug(String slug);
@Query("select m from MarketEntity m where m.status = :status")
Page<MarketEntity> findByStatus(@Param("status") MarketStatus status, Pageable pageable);
}
- 轻量级查询使用投影:
public interface MarketSummary {
Long getId();
String getName();
MarketStatus getStatus();
}
Page<MarketSummary> findAllBy(Pageable pageable);
事务
- 在服务方法上使用
@Transactional - 为优化读取路径使用
@Transactional(readOnly = true) - 谨慎选择传播行为。避免长时间运行的事务
@Transactional
public Market updateStatus(Long id, MarketStatus status) {
MarketEntity entity = repo.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Market"));
entity.setStatus(status);
return Market.from(entity);
}
分页
PageRequest page = PageRequest.of(pageNumber, pageSize, Sort.by("createdAt").descending());
Page<MarketEntity> markets = repo.findByStatus(MarketStatus.ACTIVE, page);
对于类似游标的分页,在JPQL中使用 id > :lastId 进行排序。
索引与性能
- 为常见过滤条件(
status、slug、外键)添加索引 - 使用与查询模式匹配的复合索引(如
status, created_at) - 避免
select *,仅投影所需列 - 使用
saveAll和hibernate.jdbc.batch_size进行批量写入
连接池(HikariCP)
推荐属性:
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.validation-timeout=5000
对于PostgreSQL LOB处理,添加:
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
缓存
- 一级缓存是每个EntityManager的。不要在事务之间保持实体
- 对于读取密集型实体,谨慎考虑二级缓存。验证驱逐策略
迁移
- 使用Flyway或Liquibase。不要在生产环境中依赖Hibernate自动DDL
- 保持迁移幂等且增量。不要在没有计划的情况下删除列
数据访问测试
- 优先使用Testcontainers的
@DataJpaTest以反映生产环境 - 使用日志断言SQL效率:设置
logging.level.org.hibernate.SQL=DEBUG和logging.level.org.hibernate.orm.jdbc.bind=TRACE以获取参数值
注意:保持实体轻量级,查询有针对性,事务简短。使用获取策略和投影防止N+1问题,并为读取/写入路径创建索引。