Billing System using Spring Boot Generic JpaRepository fix and Generic Code Generator

Billing System using Spring Boot Generic JpaRepository fix and Generic Code Generator
Welcome back, friends. In this article, we will be going to see how to create a Generic code generator for all tables in the Billing System and I was facing an error while auto-wiring JPARepository in BillingBaseService. Let see these two updates part of this Story. Also the same story I have posted as YouTube Video , if you want to watch the video, please click the YouTube video link below.
In BillingBaseService I had declared repository object like below.
private JpaRepository<T,ID> repository;

It was working fine and able to do CRUD Operation, I mean, I am able to do Customer, Employee, and Vendor registration. When I start adding EmploeeYRepository and another repository which extends from JpaRepository, BillingBaseService stopped working.
It was giving like this error.

Field repository in com.itgarden.service.BillingBaseService required a single bean, but 4 were found:
- abstractBaseRepository: defined in com.itgarden.repository.AbstractBaseRepository defined in
@EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
- roleRepository: defined in com.itgarden.repository.RoleRepository defined in
@EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
- appEntityCodeRepository: defined in com.itgarden.repository.AppEntityCodeRepository defined in
@EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
- employeeRepository: defined in com.itgarden.repository.EmployeeRepository defined in
@EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration

Spring Boot was saying that to fix this error, you need to use @Primary or @Qualifier Annotation.
I have tried with @Primary Annotation. I have added the @Priamry annotation in EmploeeRepository then the issue was fixed. Through this fix, I understand that when I create multiple Repository classes using JpaRepository, it creates one bean reference and that reference uses in all repository interface. and I am using the same JpaRepository reference in BillingBaseService so, Spring boot doesn’t know where need to keep the JpaRepository reference so it throws the error. After using @Primary Annotation, the issue is fixed. This is my assumption. Anyone can explain why it fixed.

I am not satisfied with this fix. so I have come up with the following way to fix it. I have created a Generic Repository class mentioned below and I declared that Repository type in BillingBaseService. After doing this, the error was fixed. Now I am totally satisfied with this fix.

public interface AbstractBaseRepository<T extends BaseObject, ID extends Serializable>
extends JpaRepository<T, ID> {

This is the first update.

Now we will be going to check Generic code generate. If we want to create a Unique code for across the system tables. there are many inbuilt ways to do in Hibernate but I don’t like those ways. I wanted to use my own logic and I would like to keep it simple.

I have created a simple table called “app_entity_code”. The table structure is below.

CREATE TABLE app_entity_code (
code_type VARCHAR(20) NOT NULL,
date_created DATETIME NOT NULL,
date_modified DATETIME NULL,
is_deleted TINYINT NULL,

This table maintains the all unique code. As of now, we need to generate Employee code, Customer code, and Vendor code. I am planning to store all unique codes in this table. It is a very simple table, it has the “code” column and “code_type” column. the Code has actual code that generated by the system and code_type says that what is the Code type for the code whether it is employee code or vendor code or customer code.

The following code is to generate the code based on the parameter we are sending.

public String newCode(CodeType codeType) {
String code = "";
AppEntityCode appEntityCode = null;
do {
String newCode = getCode();
appEntityCode = appEntityCodeRepository.findByCode(newCode);
if(appEntityCode == null) {
code = newCode;
}while(appEntityCode != null);
appEntityCode = new AppEntityCode();
return appEntityCode.getCode();

If we send “EMPLOYEE_CODE”, it generates the code and store the code in app_entity_code and store this code type as Employee code like below.
Generic code generator in Spring Boot

Whenever registration has happened, if the registration is employee, the EMPLOYEE_CODE passed as a parameter in the “newCode(String codeType)” method and generates a unique code and store it to app_entity_code. Then, assign newly generated code to employee record that currently creating. In this way, I can reuse this method for all the tables in the system.

Soon I will come up with good updates on the Billing System.

The same story I have posted as YouTube Video. Please watch this full video and share this channel with who wanted to learn how to implement a real-time project using Spring Boot.
The latest code I have updated in following GitHub Repository. Pull the code and try to set up in your local System. Good Luck. Until then bye bye from Suresh

Close Menu