如何在 JPA 存储库方法中使用部分复合键?
How to use part of composite key in JPA repository methods?
我有一个带有嵌入式 ID
的 class
@Entity
@Table(name="account_table")
Public class AccountLink {
@EmbeddedId
private AccountLinkKey accountLinkKey;
//Rest of code
}
@Embeddable
Public class AccountLinkKey {
//Rest code here
@ColumnName(name="i am hiding")
private String segAccount;
@ColumnName(name="i am hiding")
private String secAccount;
//Rest of code
}
现在在我的存储库中我想有一种方法只能通过 secAccount
进行搜索,那么我应该如何写 findBy..
List<AccountLink> findBy...(String secAccount);
我尝试了 findByAccountLinkKeySecAccount(String secAccount)
但仍然没有成功。
我已经重写了你的 class 并且还可以添加存储库功能。这是我的实现,您可能会考虑看一看。
这是 class AccountLink.java
。
@Entity
@Table(name = "account_table")
public class AccountLink {
@Column(name = "id")
public Long id;
@EmbeddedId
private AccountLinkKey accountLinkKey;
@Embeddable
public class AccountLinkKey {
@Column(name = "seg_account")
private String segAccount;
@Column(name = "sec_account")
private String secAccount;
}
}
和 AccountLinkRepository.java
。
public interface AccountLinkRepository extends JpaRepository<AccountLink, Long> {
AccountLink findAccountLinkByAccountLinkKey_SecAccount(String secAccount);
}
我在 AccountLink
class 中添加了一个 id
列作为 account_table
的主键。您的 table 可能也需要这样的东西。
通常当您想对 JPA 中的嵌入式列执行某些操作时,您可以使用下划线 ('_') 绑定您的列。
示例:
findBy<Entity_Class_Column_Name>_<Embaded_Class_Column_Name>(...)
当我们为 @Embeded
的键使用 @Entity
的删除方法时很有用。
- 您的密钥应在 Embeddable class 变量后附加“_”。
示例:pkId_firstName(pkid 是 @Embeded 的键,firstName 来自 Embedded class 变量。)
- 它应该使用键遍历到带有“下划线符号”的变量。因为没有 '_' 下划线它不起作用。
我用了这个例子:https://www.baeldung.com/spring-jpa-embedded-method-parameters
您为 id 定义复合键 Class:
@Embeddable
public class BookId implements Serializable {
private String author;
private String name;
// standard getters and setters
}
您将其用作实体中的 ID:
@Entity
public class Book {
@EmbeddedId
private BookId id;
private String genre;
private Integer price;
//standard getters and setters
}
您终于调用了存储库:
@Repository
public interface BookRepository extends JpaRepository<Book, BookId> {
List<Book> findByIdName(String name);
List<Book> findByIdAuthor(String author);
}
我使用它的代码示例:Quiz Engine with JetBrainsAcademy
我有一个带有嵌入式 ID
的 class@Entity
@Table(name="account_table")
Public class AccountLink {
@EmbeddedId
private AccountLinkKey accountLinkKey;
//Rest of code
}
@Embeddable
Public class AccountLinkKey {
//Rest code here
@ColumnName(name="i am hiding")
private String segAccount;
@ColumnName(name="i am hiding")
private String secAccount;
//Rest of code
}
现在在我的存储库中我想有一种方法只能通过 secAccount
进行搜索,那么我应该如何写 findBy..
List<AccountLink> findBy...(String secAccount);
我尝试了 findByAccountLinkKeySecAccount(String secAccount)
但仍然没有成功。
我已经重写了你的 class 并且还可以添加存储库功能。这是我的实现,您可能会考虑看一看。
这是 class AccountLink.java
。
@Entity
@Table(name = "account_table")
public class AccountLink {
@Column(name = "id")
public Long id;
@EmbeddedId
private AccountLinkKey accountLinkKey;
@Embeddable
public class AccountLinkKey {
@Column(name = "seg_account")
private String segAccount;
@Column(name = "sec_account")
private String secAccount;
}
}
和 AccountLinkRepository.java
。
public interface AccountLinkRepository extends JpaRepository<AccountLink, Long> {
AccountLink findAccountLinkByAccountLinkKey_SecAccount(String secAccount);
}
我在 AccountLink
class 中添加了一个 id
列作为 account_table
的主键。您的 table 可能也需要这样的东西。
通常当您想对 JPA 中的嵌入式列执行某些操作时,您可以使用下划线 ('_') 绑定您的列。
示例:
findBy<Entity_Class_Column_Name>_<Embaded_Class_Column_Name>(...)
当我们为 @Embeded
的键使用 @Entity
的删除方法时很有用。
- 您的密钥应在 Embeddable class 变量后附加“_”。 示例:pkId_firstName(pkid 是 @Embeded 的键,firstName 来自 Embedded class 变量。)
- 它应该使用键遍历到带有“下划线符号”的变量。因为没有 '_' 下划线它不起作用。
我用了这个例子:https://www.baeldung.com/spring-jpa-embedded-method-parameters
您为 id 定义复合键 Class:
@Embeddable
public class BookId implements Serializable {
private String author;
private String name;
// standard getters and setters
}
您将其用作实体中的 ID:
@Entity
public class Book {
@EmbeddedId
private BookId id;
private String genre;
private Integer price;
//standard getters and setters
}
您终于调用了存储库:
@Repository
public interface BookRepository extends JpaRepository<Book, BookId> {
List<Book> findByIdName(String name);
List<Book> findByIdAuthor(String author);
}
我使用它的代码示例:Quiz Engine with JetBrainsAcademy