Home > Back-end >  Use Spring Data JPA API: How to get list of Account by tennat_id (belong to a composite primary key)
Use Spring Data JPA API: How to get list of Account by tennat_id (belong to a composite primary key)

Time:07-31

I am using Spring Boot 2.7.2 , Java/JDK 18.

Entity

@Entity
@Table(name = "account")
public class Account {
    @EmbeddedId
    private AccountId id;

    @MapsId("tenantId")
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "tenant_id", nullable = false)
    private Tenant tenant;

    @Column(name = "account_number", nullable = false, length = 32)
    private String accountNumber;

    @Column(name = "account_name", nullable = false, length = 128)
    private String accountName;

    @Column(name = "account_name_english", length = 128)
    private String accountNameEnglish;

    @Column(name = "account_name_chinese", length = 128)
    private String accountNameChinese;

    @Column(name = "account_name_korean", length = 128)
    private String accountNameKorean;

    @Column(name = "description", length = 512)
    private String description;

    @Column(name = "parent_id")
    private Integer parentId;

    @Column(name = "bkit_code_id", length = 128)
    private String bkitCodeId;

    @Column(name = "grade")
    private Integer grade;

    @Column(name = "is_parent", nullable = false)
    private Boolean isParent = false;

    @Column(name = "account_category_kind", nullable = false)
    private Integer accountCategoryKind;

    @Column(name = "is_postable_in_foreign_currency", nullable = false)
    private Boolean isPostableInForeignCurrency = false;

    @Column(name = "detail_by_account_object", nullable = false)
    private Boolean detailByAccountObject = false;

    @Column(name = "account_object_type")
    private Integer accountObjectType;

    @Column(name = "detail_by_bank_account", nullable = false)
    private Boolean detailByBankAccount = false;

    @Column(name = "detail_by_job", nullable = false)
    private Boolean detailByJob = false;

    @Column(name = "detail_by_job_kind")
    private Integer detailByJobKind;

    @Column(name = "detail_by_project_work", nullable = false)
    private Boolean detailByProjectWork = false;

    @Column(name = "detail_by_project_work_kind")
    private Integer detailByProjectWorkKind;

    @Column(name = "detail_by_order", nullable = false)
    private Boolean detailByOrder = false;

    @Column(name = "detail_by_order_kind")
    private Integer detailByOrderKind;

    @Column(name = "detail_by_contract", nullable = false)
    private Boolean detailByContract = false;

    @Column(name = "detail_by_contract_kind")
    private Integer detailByContractKind;

    @Column(name = "detail_by_expense_item", nullable = false)
    private Boolean detailByExpenseItem = false;

    @Column(name = "detail_by_expense_item_kind")
    private Integer detailByExpenseItemKind;

    @Column(name = "detail_by_department", nullable = false)
    private Boolean detailByDepartment = false;

    @Column(name = "detail_by_department_kind")
    private Integer detailByDepartmentKind;

    @Column(name = "detail_by_list_item", nullable = false)
    private Boolean detailByListItem = false;

    @Column(name = "detail_by_list_item_kind")
    private Integer detailByListItemKind;

    @Column(name = "active_status", nullable = false)
    private Boolean activeStatus = false;

    @Column(name = "created")
    private OffsetDateTime created;

    @Column(name = "created_by", length = 64)
    private String createdBy;

    @Column(name = "modified")
    private OffsetDateTime modified;

    @Column(name = "modified_by", length = 64)
    private String modifiedBy;

    @Column(name = "sort_bkit_code_id", length = 128)
    private String sortBkitCodeId;

    @Column(name = "detail_by_pu_contract", nullable = false)
    private Boolean detailByPuContract = false;

    @Column(name = "detail_by_pu_contract_kind")
    private Integer detailByPuContractKind;

    public AccountId getId() {
        return id;
    }

    public void setId(AccountId id) {
        this.id = id;
    }

    public Tenant getTenant() {
        return tenant;
    }

    public void setTenant(Tenant tenant) {
        this.tenant = tenant;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getAccountNameEnglish() {
        return accountNameEnglish;
    }

    public void setAccountNameEnglish(String accountNameEnglish) {
        this.accountNameEnglish = accountNameEnglish;
    }

    public String getAccountNameChinese() {
        return accountNameChinese;
    }

    public void setAccountNameChinese(String accountNameChinese) {
        this.accountNameChinese = accountNameChinese;
    }

    public String getAccountNameKorean() {
        return accountNameKorean;
    }

    public void setAccountNameKorean(String accountNameKorean) {
        this.accountNameKorean = accountNameKorean;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public String getBkitCodeId() {
        return bkitCodeId;
    }

    public void setBkitCodeId(String bkitCodeId) {
        this.bkitCodeId = bkitCodeId;
    }

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    public Boolean getIsParent() {
        return isParent;
    }

    public void setIsParent(Boolean isParent) {
        this.isParent = isParent;
    }

    public Integer getAccountCategoryKind() {
        return accountCategoryKind;
    }

    public void setAccountCategoryKind(Integer accountCategoryKind) {
        this.accountCategoryKind = accountCategoryKind;
    }

    public Boolean getIsPostableInForeignCurrency() {
        return isPostableInForeignCurrency;
    }

    public void setIsPostableInForeignCurrency(Boolean isPostableInForeignCurrency) {
        this.isPostableInForeignCurrency = isPostableInForeignCurrency;
    }

    public Boolean getDetailByAccountObject() {
        return detailByAccountObject;
    }

    public void setDetailByAccountObject(Boolean detailByAccountObject) {
        this.detailByAccountObject = detailByAccountObject;
    }

    public Integer getAccountObjectType() {
        return accountObjectType;
    }

    public void setAccountObjectType(Integer accountObjectType) {
        this.accountObjectType = accountObjectType;
    }

    public Boolean getDetailByBankAccount() {
        return detailByBankAccount;
    }

    public void setDetailByBankAccount(Boolean detailByBankAccount) {
        this.detailByBankAccount = detailByBankAccount;
    }

    public Boolean getDetailByJob() {
        return detailByJob;
    }

    public void setDetailByJob(Boolean detailByJob) {
        this.detailByJob = detailByJob;
    }

    public Integer getDetailByJobKind() {
        return detailByJobKind;
    }

    public void setDetailByJobKind(Integer detailByJobKind) {
        this.detailByJobKind = detailByJobKind;
    }

    public Boolean getDetailByProjectWork() {
        return detailByProjectWork;
    }

    public void setDetailByProjectWork(Boolean detailByProjectWork) {
        this.detailByProjectWork = detailByProjectWork;
    }

    public Integer getDetailByProjectWorkKind() {
        return detailByProjectWorkKind;
    }

    public void setDetailByProjectWorkKind(Integer detailByProjectWorkKind) {
        this.detailByProjectWorkKind = detailByProjectWorkKind;
    }

    public Boolean getDetailByOrder() {
        return detailByOrder;
    }

    public void setDetailByOrder(Boolean detailByOrder) {
        this.detailByOrder = detailByOrder;
    }

    public Integer getDetailByOrderKind() {
        return detailByOrderKind;
    }

    public void setDetailByOrderKind(Integer detailByOrderKind) {
        this.detailByOrderKind = detailByOrderKind;
    }

    public Boolean getDetailByContract() {
        return detailByContract;
    }

    public void setDetailByContract(Boolean detailByContract) {
        this.detailByContract = detailByContract;
    }

    public Integer getDetailByContractKind() {
        return detailByContractKind;
    }

    public void setDetailByContractKind(Integer detailByContractKind) {
        this.detailByContractKind = detailByContractKind;
    }

    public Boolean getDetailByExpenseItem() {
        return detailByExpenseItem;
    }

    public void setDetailByExpenseItem(Boolean detailByExpenseItem) {
        this.detailByExpenseItem = detailByExpenseItem;
    }

    public Integer getDetailByExpenseItemKind() {
        return detailByExpenseItemKind;
    }

    public void setDetailByExpenseItemKind(Integer detailByExpenseItemKind) {
        this.detailByExpenseItemKind = detailByExpenseItemKind;
    }

    public Boolean getDetailByDepartment() {
        return detailByDepartment;
    }

    public void setDetailByDepartment(Boolean detailByDepartment) {
        this.detailByDepartment = detailByDepartment;
    }

    public Integer getDetailByDepartmentKind() {
        return detailByDepartmentKind;
    }

    public void setDetailByDepartmentKind(Integer detailByDepartmentKind) {
        this.detailByDepartmentKind = detailByDepartmentKind;
    }

    public Boolean getDetailByListItem() {
        return detailByListItem;
    }

    public void setDetailByListItem(Boolean detailByListItem) {
        this.detailByListItem = detailByListItem;
    }

    public Integer getDetailByListItemKind() {
        return detailByListItemKind;
    }

    public void setDetailByListItemKind(Integer detailByListItemKind) {
        this.detailByListItemKind = detailByListItemKind;
    }

    public Boolean getActiveStatus() {
        return activeStatus;
    }

    public void setActiveStatus(Boolean activeStatus) {
        this.activeStatus = activeStatus;
    }

    public OffsetDateTime getCreated() {
        return created;
    }

    public void setCreated(OffsetDateTime created) {
        this.created = created;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public OffsetDateTime getModified() {
        return modified;
    }

    public void setModified(OffsetDateTime modified) {
        this.modified = modified;
    }

    public String getModifiedBy() {
        return modifiedBy;
    }

    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public String getSortBkitCodeId() {
        return sortBkitCodeId;
    }

    public void setSortBkitCodeId(String sortBkitCodeId) {
        this.sortBkitCodeId = sortBkitCodeId;
    }

    public Boolean getDetailByPuContract() {
        return detailByPuContract;
    }

    public void setDetailByPuContract(Boolean detailByPuContract) {
        this.detailByPuContract = detailByPuContract;
    }

    public Integer getDetailByPuContractKind() {
        return detailByPuContractKind;
    }

    public void setDetailByPuContractKind(Integer detailByPuContractKind) {
        this.detailByPuContractKind = detailByPuContractKind;
    }

}
@Embeddable
public class AccountId implements Serializable {
    private static final long serialVersionUID = 2728412978200770912L;
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "tenant_id", nullable = false)
    private Integer tenantId;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getTenantId() {
        return tenantId;
    }

    public void setTenantId(Integer tenantId) {
        this.tenantId = tenantId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        AccountId entity = (AccountId) o;
        return Objects.equals(this.tenantId, entity.tenantId) &&
                Objects.equals(this.id, entity.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(tenantId, id);
    }

}

My services

public interface AccountService {

    // FIXME: Need get Account belong to tenant_id .
    List<Account> getAll();
}

and

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountRepository accountRepository;

    // FIXME: Need get Account belong to tenant_id .
    @Override
    public List<Account> getAll() {
        return accountRepository.findAll(); // <-- Need revise.
    }

}

How to find all accounts by a tenant_id? I can use native SQL query, but I want use Spring Data JPA API more than.

CodePudding user response:

You can use Spring Data Repository query keywords on your composite key by find all accounts by following code.

@Repository
public interface AccountRepository extends JpaRepository<Account, AccountId> {
    List<Account> findAllByTenantId(Integer tenant_id);
}
  • Related