Can I create a spring-boot library that contains JPA entities, repositories and services that can be subclassed?

Viewed 425

I'm working on building a library that will be used in a few other spring-boot applications. I'm trying to consolidate a lot of the code that will be repeated across them.

What I've tried so far I'm running into problems with returning the right type of object (the one from the library or the application) and I keep getting an Unable to locate persister exception.

I will have Controllers, Services, JPA Entities and Repositories defined in the libraries many of which will be inherited by other classes in the application but I need to be able to use those Entities and Repositories in the library.

For example I might have an account entity in the Library

package library;

//imports

@MappedSuperclass
@Data
public class AccountBase {
  public Long id;
  public String username;
  public String password;
}

In the library I will also have associated Repository and Service in the library.

Repository:

package library;

//imports

@Repository
@Transactional
public interface AccountRepositoryBase extends JpaRepository<AccountBase, UUID>{
}

Service:

package library;

//imports

@Service
@Transactional
public class AccountServiceBase {
    @Autowired
    protected AccountRepositoryBase accountRepositoryBase;

    public List<AccountBase> listAll() {
        List<AccountBase> accounts = accountRepositoryBase.findAll();
        return accounts;
    }
}

In a controller class in the library I will use the service class to lookup data in the account table. For example:

package library;

//imports

public class BaseController {
  @Autowired AccountService accountService;
  public List<Account> getAccounts() {
     return accountService.listAll();
  }
}

In the application I'll add some more attributes to Account such as

package application;

//imports

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Entity
@Data
public class Account extends library.AccountBase {
  public String emailAddress;
  public String phoneNumber;
}

When I run the application the main exception I'm seeing now is Unable to locate persister library.AccountBase

It feels like I'm doing this whole thing the wrong way?

0 Answers
Related