0

I use Spring Boot 3 + Data JPA + PostgreSQL and having a problem while starting the application. The error appears when I add repository files.

BaseEntity class

package com.example.contactc.entity;

import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.FieldDefaults;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import java.sql.Timestamp;


@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level= AccessLevel.PRIVATE)
public class BaseEntity {

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    Timestamp cdt;

    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    Timestamp mdt;

    @Temporal(TemporalType.TIMESTAMP)
    Timestamp rdt;

    public void markDeleted(){
        this.setRdt(new Timestamp(System.currentTimeMillis()));
    }
}

Entity class

package com.example.contactc.entity;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;


@Data
@Entity
@Table(name = "branch")
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
@FieldDefaults(level= AccessLevel.PRIVATE)
public class Branch extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @Column(name = "name")
    String name;

}

BaseService class

package com.example.contactc.service;

import com.example.contactc.entity.BaseEntity;
import com.example.contactc.model.BaseModel;

public interface BaseService<T extends BaseModel, E extends BaseEntity, ID> {
    T findById(ID modelId);
    E save(T model);

    T deleteById(ID modelId);
    T toModel(E entity);
}

BranchService class

package com.example.contactc.service;

import com.example.contactc.entity.Branch;
import com.example.contactc.model.BranchModel;

public interface BranchService extends BaseService<BranchModel, Branch, Long> {
}

BranchServiceImpl class

package com.example.contactc.service.impl;

import com.example.contactc.entity.Branch;
import com.example.contactc.exception.RecordNotFoundException;
import com.example.contactc.model.BranchModel;
import com.example.contactc.repo.BranchRepo;
import com.example.contactc.service.BranchService;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.springframework.stereotype.Service;


@Service
@AllArgsConstructor
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class BranchServiceImpl implements BranchService {

    BranchRepo repo;

    @Override
    public BranchModel findById(Long modelId) {
        return repo.findByIdAndRdtIsNull(modelId).map(this::toModel)
                .orElseThrow(() -> new RecordNotFoundException(Branch.class, modelId));
    }

    @Override
    public Branch save(BranchModel model) {
        var branch = model.getId() == null ? new Branch() :
                repo.findByIdAndRdtIsNull(model.getId())
                        .orElseThrow(() -> new RecordNotFoundException(Branch.class, model.getId()));

        branch.setName(model.getName());

        return repo.save(branch);
    }

    @Override
    public BranchModel deleteById(Long modelId) {
        var branch = repo.findByIdAndRdtIsNull(modelId).orElseThrow(() -> new RecordNotFoundException(Branch.class, modelId));
        branch.markDeleted();
        return toModel(repo.save(branch));
    }

    @Override
    public BranchModel toModel(Branch entity) {
        return BranchModel.builder()
                .id(entity.getId())
                .name(entity.getName())
                .cdt(entity.getCdt())
                .mdt(entity.getMdt())
                .rdt(entity.getRdt())
                .build();
    }
}

Repository class

package com.example.contactc.repo;

import com.example.contactc.entity.Branch;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface BranchRepo extends JpaRepository<Branch, Long> {
    Optional<Branch> findByIdAndRdtIsNull(Long id);
}

Application class

package com.example.contactc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
public class ContactCApplication {

    public static void main(String[] args) {
        SpringApplication.run(ContactCApplication.class, args);
    }

}

application.yml file

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/contactcdb
    username: tkasenov
    password:
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ContactC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ContactC</name>
    <description>ContactC</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>3.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

my packages structure enter image description here

I have tried to add this annotations

@EnableJpaRepositories("com.example.*")
@ComponentScan(basePackages = { "com.example.*" })
@EntityScan("com.example.*")

but it had no effect the error

0 Answers0