Home > Software design >  Hibernate cannot set values by Lombok setter
Hibernate cannot set values by Lombok setter

Time:09-27

I have entity class representing post as below.

@Entity
@Getter
@Setter
public class Post {
  @Id
  private long id;
  private String title;
  private String content;
  private LocalDateTime created;
}

When I use jpaRepository to findAll posts I receive list of empty elements.

Empty post response

JpaRepository code :

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {}

PostService :

@Service
public class PostService {

@Autowired
private PostRepository postRepository;

   public List<Post> getPosts() {
   return postRepository.findAll();
}
}

I found that Hibernate propably cannot set values of Post that's why I got list of empty elements. If I change post properties to public, everything works fine but I want to keep they private. I guess @Setter annotation doesn't work and hibernate can't parse data into Post objects.

Can someone know how to fix it?

My gradle dependency section

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
implementation 'io.springfox:springfox-swagger2:3.0.0'
implementation 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok:1.18.24'

testImplementation 'org.springframework.boot:spring-boot-starter-test'

}

CodePudding user response:

change long to Long on entity level

CodePudding user response:

Try to use the below annotations instead:

@NoArgsConstructor
@Getter
@Setter
  • Related