3

I am playing with the Lombok and already went through many link but none of them worked for me.

Person.java

@Setter @Getter
@ToString
@AllArgsConstructor
//@NoArgsConstructor
@RequiredArgsConstructor
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    @Size(min = 1, max = 20)
    private String firstName;

    @NotNull
    @Size(min = 1, max = 50)
    private String lastName;
}

PersonController.java

@RestController
@RequestMapping("/people")
public class PersonController {
    @Autowired
    private PersonRepository personRepository;

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public void createPerson(@RequestBody Person person) {
        personRepository.save(new Person(person.getFirstName(), person.getLastName()));  //line-34
    }
}

But its not allowing me to create the two argument constructor

Multiple markers at this line
    - The constructor Person(String, String) is undefined
    - The method save(S) in the type CrudRepository<Person,Long> is not applicable for the arguments 
     (Person)

On line No-34 its breaking...

EDIT-1:

 @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updatePerson(@PathVariable("id") Long id, @RequestBody Person person) {
        Person existingPerson = personRepository.findOne(id);
        existingPerson.setFirstName(person.getFirstName());
        existingPerson.setLastName(person.getLastName());
        personRepository.save(existingPerson);
    }

Here is the error

The method setFirstName(String) is undefined for the type Person

The changes I made

@Setter @Getter
@ToString
@AllArgsConstructor
//@NoArgsConstructor
@RequiredArgsConstructor()
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    @Size(min = 1, max = 20)
    private final String firstName;

    @NotNull
    @Size(min = 1, max = 50)
    private final String lastName;
} 

-===================

Edit-2

Here is the final result:

@Setter @Getter
@ToString
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    @Size(min = 1, max = 20)
    private String firstName;

    @NotNull
    @Size(min = 1, max = 50)
    private String lastName;

    public Person(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
PAA
  • 1
  • 46
  • 174
  • 282
  • Avoid using @EqualsAndHashCode on JPA entities. Read the links in this answer: https://stackoverflow.com/a/34299054/1331935 – Jeff Oct 30 '17 at 16:14

1 Answers1

3

You do not have Lomboks @NonNull annotation on those fields. I noticed it just now.

You have only @javax.validation.constraints.NotNull annotation on those fieldṣ and @RequiredArsgConstructor does not apply on that.

Add the @NonNull in addition to the @NotNull annotation. It might be that you do not need the @NotNull anymore so try to remove it also.

@NonNull 
@NotNull
@Size(min = 1, max = 20)
private String firstName;

@NonNull 
@NotNull
@Size(min = 1, max = 50)
private String lastName;
pirho
  • 11,565
  • 12
  • 43
  • 70
  • But its breaking my other code shown above in edit. Please show the code snippet, so that I can understand. – PAA Oct 30 '17 at 14:40
  • Then what would be the solution ? Looks like Lombok approach will not suffice for the Jpa and mongo – PAA Oct 30 '17 at 14:49
  • sorry for the misunderstanding, i read the lombok docs again and saw the actual problem. – pirho Oct 30 '17 at 15:04
  • Hey, I dont understand your last comment. What do I need to change in that model class ? Please guide – PAA Oct 30 '17 at 16:13