使用这样的实体:
public class AccountId implements Serializable {
private int a;
private int b;
// getters, setters, etc.
}
@Entity
@IdClass(AccountId.class)
public class Account implements Serializable {
@Id
private int a;
@Id
private int b;
// getters, setters, etc.
}
是否可以使用复合键 ( AccountId
) 编写标准 JPA 存储库:
public interface AccountRepository extends CrudRepository<Account, AccountId> {
void findByCompositeIdAnd<some other conditions>(AccountId accountId, <other paramters>);
}
而不是关键组件(a
和b
):
public interface AccountRepository extends CrudRepository<Account, AccountId> {
void findByCompositeIdAnd<some other conditions>(int a, int b, <other paramters>);
}
我想避免使用@EmbeddedId
to 减少冗长或编写 custom @Query
。