java-jpa-hibernate by pluginagentmarketplace/custom-plugin-java
npx skills add https://github.com/pluginagentmarketplace/custom-plugin-java --skill java-jpa-hibernate掌握 JPA 和 Hibernate,为生产级应用程序实现数据持久化。
本技能涵盖 JPA 实体设计、Hibernate 优化、Spring Data 仓库、查询策略和缓存。重点在于防止 N+1 查询和构建高性能的持久化层。
在以下情况下使用:
// Entity with relationships
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
@BatchSize(size = 20)
private List<OrderItem> items = new ArrayList<>();
@Version
private Long version;
// Bidirectional helper
public void addItem(OrderItem item) {
items.add(item);
item.setOrder(this);
}
}
// Auditing base class
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable {
@CreatedDate
@Column(updatable = false)
private Instant createdAt;
@LastModifiedDate
private Instant updatedAt;
}
// Repository with query optimization
public interface OrderRepository extends JpaRepository<Order, Long> {
// JOIN FETCH to prevent N+1
@Query("SELECT DISTINCT o FROM Order o JOIN FETCH o.items WHERE o.status = :status")
List<Order> findByStatusWithItems(@Param("status") Status status);
// EntityGraph alternative
@EntityGraph(attributePaths = {"items", "customer"})
Optional<Order> findById(Long id);
// DTO Projection
@Query("SELECT new com.example.OrderSummary(o.id, o.status, c.name) " +
"FROM Order o JOIN o.customer c WHERE o.id = :id")
Optional<OrderSummary> findSummaryById(@Param("id") Long id);
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 策略 | 适用场景 | 示例 |
|---|---|---|
| JOIN FETCH | 总是需要关联数据 | JOIN FETCH o.items |
| EntityGraph | 动态获取 | @EntityGraph(attributePaths) |
| @BatchSize | 集合访问 | @BatchSize(size = 20) |
| DTO 投影 | 只读查询 | new OrderSummary(...) |
spring:
jpa:
open-in-view: false # Critical!
properties:
hibernate:
jdbc.batch_size: 50
order_inserts: true
order_updates: true
default_batch_fetch_size: 20
generate_statistics: ${HIBERNATE_STATS:false}
datasource:
hikari:
maximum-pool-size: 20
minimum-idle: 5
leak-detection-threshold: 60000
| 问题 | 原因 | 解决方案 |
|---|---|---|
| N+1 查询 | 循环中的延迟加载 | JOIN FETCH, EntityGraph |
| LazyInitException | 会话已关闭 | DTO 投影 |
| 查询缓慢 | 缺少索引 | EXPLAIN ANALYZE |
| 连接泄漏 | 缺少 @Transactional | 添加注解 |
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.orm.jdbc.bind=TRACE
hibernate.generate_statistics=true
□ 启用 SQL 日志
□ 检查每个请求的查询数量
□ 验证获取策略
□ 检查 @Transactional
□ 检查连接池
Skill("java-jpa-hibernate")
java-performance - 查询优化java-spring-boot - Spring Data每周安装量
230
代码仓库
GitHub 星标数
27
首次出现
2026年1月21日
安全审计
安装于
opencode189
codex177
gemini-cli176
github-copilot164
cursor144
amp141
Master data persistence with JPA and Hibernate for production applications.
This skill covers JPA entity design, Hibernate optimization, Spring Data repositories, query strategies, and caching. Focuses on preventing N+1 queries and building high-performance persistence layers.
Use when you need to:
// Entity with relationships
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id", nullable = false)
private Customer customer;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
@BatchSize(size = 20)
private List<OrderItem> items = new ArrayList<>();
@Version
private Long version;
// Bidirectional helper
public void addItem(OrderItem item) {
items.add(item);
item.setOrder(this);
}
}
// Auditing base class
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable {
@CreatedDate
@Column(updatable = false)
private Instant createdAt;
@LastModifiedDate
private Instant updatedAt;
}
// Repository with query optimization
public interface OrderRepository extends JpaRepository<Order, Long> {
// JOIN FETCH to prevent N+1
@Query("SELECT DISTINCT o FROM Order o JOIN FETCH o.items WHERE o.status = :status")
List<Order> findByStatusWithItems(@Param("status") Status status);
// EntityGraph alternative
@EntityGraph(attributePaths = {"items", "customer"})
Optional<Order> findById(Long id);
// DTO Projection
@Query("SELECT new com.example.OrderSummary(o.id, o.status, c.name) " +
"FROM Order o JOIN o.customer c WHERE o.id = :id")
Optional<OrderSummary> findSummaryById(@Param("id") Long id);
}
| Strategy | Use When | Example |
|---|---|---|
| JOIN FETCH | Always need relation | JOIN FETCH o.items |
| EntityGraph | Dynamic fetching | @EntityGraph(attributePaths) |
| @BatchSize | Collection access | @BatchSize(size = 20) |
| DTO Projection | Read-only queries | new OrderSummary(...) |
spring:
jpa:
open-in-view: false # Critical!
properties:
hibernate:
jdbc.batch_size: 50
order_inserts: true
order_updates: true
default_batch_fetch_size: 20
generate_statistics: ${HIBERNATE_STATS:false}
datasource:
hikari:
maximum-pool-size: 20
minimum-idle: 5
leak-detection-threshold: 60000
| Problem | Cause | Solution |
|---|---|---|
| N+1 queries | Lazy in loop | JOIN FETCH, EntityGraph |
| LazyInitException | Session closed | DTO projection |
| Slow queries | Missing index | EXPLAIN ANALYZE |
| Connection leak | No @Transactional | Add annotation |
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.orm.jdbc.bind=TRACE
hibernate.generate_statistics=true
□ Enable SQL logging
□ Check query count per request
□ Verify fetch strategies
□ Review @Transactional
□ Check connection pool
Skill("java-jpa-hibernate")
java-performance - Query optimizationjava-spring-boot - Spring DataWeekly Installs
230
Repository
GitHub Stars
27
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode189
codex177
gemini-cli176
github-copilot164
cursor144
amp141
GSAP React 动画库使用指南:useGSAP Hook 与最佳实践
1,700 周安装
Next.js 最佳实践指南:App Router 开发原则、性能优化与项目结构
3,800 周安装
Reka UI - 无样式、可访问的 Vue 3 组件库 | 符合 WAI-ARIA 标准
3,800 周安装
核心Web指标优化指南:LCP、INP、CLS三大性能指标详解与实战技巧
3,900 周安装
Angular 路由配置指南:v20+ 懒加载、Signal 参数与函数式守卫
3,800 周安装
Angular Signal Forms 教程:构建类型安全响应式表单的完整指南
3,900 周安装
React现代化升级指南:版本迁移、Hooks转换、并发特性与自动化重构
4,000 周安装