如何将 @IdClass 与 JPA 存储库一起使用? [英] How do I use @IdClass with JPA Repository?

查看:42
本文介绍了如何将 @IdClass 与 JPA 存储库一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Spring JPA Repository 接口来持久化对象,而且效果很好!但是当 @Entity 定义了一个 @IdClass 时,我该如何使用它呢?类未嵌入,但字段是对象的一部分.我应该创建命名所有复合字段的 findBy 方法吗?或者有没有办法创建一个findById并传递PK类?

I've been using a Spring JPA Repository interface to persist objects, and it works great! But how do I use it when the @Entity defines an @IdClass? The class is not embedded, but the fields are part of the object. Is it expected I should create findBy methods that name all the composite fields? Or is there a way to create a findById and pass the PK class?

推荐答案

没关系 - 我找到了我遗漏的东西.当我真正想要的是 org.springframework.data.jpa.repository.JpaRepository 时,我正在扩展 org.springframework.data.repository.Repository.它有一个用于 findOne 的内置方法,它接受 PK 类.

Nevermind - I found what I was missing. I was extending org.springframework.data.repository.Repository when what I really wanted was org.springframework.data.jpa.repository.JpaRepository. It has a builtin method for findOne which accepts the PK class.

这篇关于如何将 @IdClass 与 JPA 存储库一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆