2

I have Spring Boot Application with Web, JPA, H2, Web, Lombok dependency. I have entities as follows

@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Getter
public class Book {

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

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private Integer pageCount;

    @ManyToOne
    @JoinColumn(name = "author_id")
    private Author author;

}
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Getter
public class Author {

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

    @Column(nullable = false)
    private String firstName;

    @Column
    private String lastName;

}

I am getting following JSON in repose for the /books REST endpoint

[
    {},
    {}
]

If I add getters and setters to entity it works fine. How can I get the actual values in response without adding getters setters as I am using @Data annotation

Tushar Girase
  • 183
  • 4
  • 15
  • 1
    You don't need Getter and Data at the same time, Data already sets up your getters. Also Entity should be used with Table(name = "table_name" ) annotation. – Berk Kurkcuoglu Jun 03 '20 at 08:59
  • You shouldn't use @Data at all on JPA types since the 'toString', 'equals', and 'hashCode' need to be handled properly. Please see my answer for details https://stackoverflow.com/a/34299054/1331935. – Jeff Jun 05 '20 at 15:25

5 Answers5

3

First of all , In spring it is not a good approach to return the entity object directly from a controller to be converted to json. Use the DTO approach so that you will always have a DTO object returned from the controller endpoint. This way you will have more control over the type and structure of data you want to retun from the endpoint.

Read here about the advantages of using a DTO .

Secondly, verify that your tables follow the basic spring jpa naming conventions or is same as the entity class names if not please add the @Table(name="") annotation to specify the table name. Check that data is being populated to your entity classes.

Remove the @Data annotation.

@Data should not be used on JPA Entities as Entity toString, equals, and hashCode methods need to be authored in a very specific manner. See here for details.

Jeff
  • 3,712
  • 2
  • 22
  • 24
Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
  • Thanks for the answer it helped me to understand the standard implementations. – Tushar Girase Jun 03 '20 at 09:59
  • Are the entities retrieving data from the database ? Does you entity class have any data when being returned from the controller ? Could you share your code . – Ananthapadmanabhan Jun 03 '20 at 10:20
  • If I add the getters and setters it works fine. I checked after adding those I was getting the desired output. – Tushar Girase Jun 03 '20 at 10:25
  • You shouldn't use @Data at all on JPA types since the 'toString', 'equals', and 'hashCode' need to be handled properly. Please see my answer for details https://stackoverflow.com/a/34299054/1331935. I will update this answer. – Jeff Jun 05 '20 at 15:27
2

After searching a lot and looking on the answers to my question found a solution. If I run my spring boot app using java -jar from cmd it works fine. But not with IDE so issue was with IDE configuration.

I am using Eclipse IDE

Right Click on your project -> Properties

Then from Properties window

Java Compiler -> Annotation Processing -> Enable Annotation Processing

This option should be checked then annotations gets processed. I followed https://www.baeldung.com/lombok-ide from Tinku's answer on this question.

enter image description here

Tushar Girase
  • 183
  • 4
  • 15
1

need to add Lombok plugin in maven or gradle file link How to configure Lombok with maven-compiler-plugin?. If you are using IDE then need to install Lombok plugin link https://www.baeldung.com/lombok-ide

Tinku
  • 751
  • 5
  • 19
0

If you are using Eclipse check to have Lombok installed:

enter image description here

Florin Zfs
  • 21
  • 3
0

I'm using IntelliJ IDEA, but neither enabling the Annotation Processing nor changing my code (e.g. remove @Data) helped me to solve this issue.

I had added this line to my build.gradle

compileOnly 'org.projectlombok:lombok:1.18.28'

But I was missing the following one to make it work:

annotationProcessor 'org.projectlombok:lombok:1.18.28'
Acipenser
  • 1
  • 2