Search code examples
javaspring-bootviewspring-data-jpamaterialized-views

Issue while trying to implement materialized view with Spring JPA


I'm trying to implement Materialized view with Spring JPA in my project by following multiple guides online. The Materialized view is created by using Liquibase as shown below. The Entity code for the View and the corresponding repositories and service class code are also shown below.

CREATE MATERIALIZED VIEW IF NOT EXISTS summary
AS
SELECT personal_details.id_no, address.address from personal_details
JOIN address
ON personal_details.id_no = address.id_no
ORDER BY personal_details.id_no;
@Entity
@Immutable
@Getter
@Table(name = "summary")
public class Summary {
    @Id
    @Column(name = "id_no")
    private String idNo;

    @Column(name = "address")
    private String address;
}
@NoRepositoryBean
public interface ReadOnlyRepository<T, ID> extends Repository<T, ID> {

    List<T> findAll();

    List<T> findAll(Sort sort);

    Page<T> findAll(Pageable pageable);

    Optional<T> findById(ID id);

    long count();
}
public interface SummaryRepository extends ReadOnlyRepository<Summary, String> {
    List<Summary> findByidNo(String idNo);
}
@Service
public class SummaryServiceImpl implements SummaryService {
    @Autowired
    private SummaryRepository summaryRepository;

    @Override
    public List<Summary> test() {
        return summaryRepository.findAll();
    }
}

However, I'm always getting an error while trying to run the program. What am I doing wrong here?

Description:

Field summaryRepository in services.impl.SummaryServiceImpl required a bean of type 'repositories.SummaryRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'repositories.SummaryRepository' in your configuration.


Process finished with exit code 1

EDIT: If I extend my repository directly from a JpaRepository, as shown below:

@Repository
public interface SummaryRepository extends JpaRepository<Summary, String> {
    List<Summary> findByidNo(String idNo);
}

I get a different type of error:

Error creating bean with name 'Summary Data': Unsatisfied dependency expressed through field 'summaryService': Error creating bean with name 'summaryServiceImpl': Unsatisfied dependency expressed through field 'summaryRepository': Error creating bean with name 'summaryRepository' defined in repositories.SummaryRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class entities.Summary

I'm out of ideas on how to integrate Materialized views with Spring JPA. Any help would be great.


Solution

  • The error appears to hint on mixing a newer Spring Boot version 3+ which is JakartaEE only with JavaEE (old JPA annotations). That won't work as it won't detect the entities.

    Switch your imports to jakarta.persistence and remove the dependency that allowed you to do that in the first.