0

I have a Hibernate model with id, name and surname. I am using it to get data from the database and then at the GET end-point is this one:

@GetMapping(value = "/contacts", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Contact> allContacts() {
    return contactService.findAll();
}

As you can see it returns the Contact object. Actually it is a Hibernate entity.

The problem is that when I use this code

@PostMapping("/contacts")
public Contact createContact(Contact contact) {
    return contactService.createContact(contact);
}

it asks not only name and surname but also the id. POST methods should not ask for id since they are not created yet. What should I do so that it doesn't ask for an id?

Edit: Here is the Contact.java class

import lombok.Data;
import javax.persistence.*;

@Entity
@Data
public class Contact {

    public Contact() {
    }

    public Contact(Integer id, String name, String surname) {
        this.id = id;
        this.name = name;
        this.surname = surname;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private Integer id;

    private String name;

    private String surname;

}
ilhan
  • 8,700
  • 35
  • 117
  • 201
  • you should use DTOs and leave entities only in persistence layer for clean architecture. Following that, you will no longer face this issue. – Lkopo Oct 27 '20 at 21:10
  • 1
    How is Contact mapped to your table? Do you have a @GeneratedValue to assign it an id from a sequence or similar? Can you show the source for Contact? – Kevin Hooke Oct 27 '20 at 21:11
  • 1
    @Lkopo that is not true and it **very much** depends on the scenario. Entities can be perfectly well used as a data model which is sent and received, and there is nothing wrong with that. [Here](https://stackoverflow.com/a/63649787/1553537) I explain this in a deeper details. – Giorgi Tsiklauri Oct 27 '20 at 21:14
  • @ilhan can you check the `@Entity class Contact{..}`'s `id`, what is a strategy it uses for ID generation? or, please, just show us the `Contact` class. – Giorgi Tsiklauri Oct 27 '20 at 21:16

2 Answers2

1

Define a ContactInput class that only has the attributes you want the user to input and then create some mapping code that creates a valid Contact based on the ContactInput.

luk2302
  • 55,258
  • 23
  • 97
  • 137
1

You should create ContactDto class

   @Data
   public ContactDto class {
    private String name;
    private String surname;
   }

In @PostMapping you are gonna get ContactDto from the user. You cannot saved ContactDto into your database. So you need to map ContactDto to Contact. What you can do is simply create ContractMapper class.

public static contactDtoToEntity(ContactDto dto){
Contact dbContact = new Contact();
dbContact.setName(dto.getName());
dbContact.setSurname(dto.getSurname());
return dbContact;
}

Before you saved the contact in your database in service layer, you need to map it and then save. Id is gonna be generated in the database.

someone
  • 45
  • 6