'Is using id field in @AllArgsConstructor while using Spring JPA correct?
Using spring-boot and JPA I have an Entity I want to use lombok to reduce boilerplate code. However in my entity there is the id field. Shall I put it in the constructor arguments with @AllArgsConstructor or shall I eliminate it from theargument list (somehow, how?) due to being auto-generated with the @id and @GeneratedValue annotations?
code:
@Entity
@NoArgsConstructor // JPA requires empty constructor
@AllArgsConstructor // ? is id in constuctor needed?
@Getter
@Setter
@ToString(exclude = {"room", "customer"})
public class Reservation {
@Id
@GeneratedValue
private long id;
private Room room;
private Customer customer;
private Date dateFrom;
private Date dateTo;
}
Solution 1:[1]
For your question in code:
@AllArgsConstructor // ? is id in constuctor needed?
No it is not needed. Furthermore, for your question in the title:
Is using id field in @AllArgsConstructor while using Spring JPA correct?
Field id it is not recommended to be exposed to any constructor or setter unless there is a very good reason for that. Field id should be manipulated only by JPA implementation.
Note that this expose happens also when you declare @Setter on Reservation class level.
This can be avoided to remove annotation from class level and annotate each field to expose but easier way is to use inheritance.
You can create a base class like:
@Entity
@Getter
// Choose your inheritance strategy:
//@Inheritance(strategy=InheritanceType.JOINED)
//@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {
@Id
@GeneratedValue
private Long id;
}
Note that it does not have setter for field id. Extend above class like:
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString(exclude = {"room", "customer"})
public class Reservation extends BaseEntity {
private Room room;
private Customer customer;
private Date dateFrom;
private Date dateTo;
}
and the constructors & setters will be the following:
Reservation r1 = new Reservation();
Reservation r2 = new Reservation(room, customer, dateFrom, dateTo);
r1.setRoom(room);
r1.setCustomer(customer);
r1.setDateFrom(dateFrom);
r1.setDateTo(dateTo);
and there is no way - other that reflection that JPA uses - to set the field id.
I do not know how spring-data-jpa exactly does setting the id but as there is a keyword JPA and tag jpa I assumes this is a JPA thing and that setter for field id is not actually needed at all.
Solution 2:[2]
A comment for the Lombok part: There is no way to exclude field from the @AllArgsConstructor (without using inheritance). For a limited constructor, you can only use @RequiredArgsConstructor, but this will generally not help you for JPA entities.
Solution 3:[3]
In this case spring-data uses setters to set values, so @AllArgsConstructor is not needed.
Solution 4:[4]
My solution is when id is auto generated by jpa and your are using lombok to avoid boilerplate code.
@Entity
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@AllArgsConstructor
public class Patient {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long reg;
@NotNull
private String name;
@NotNull
private String gender;
@NotNull
private String specimen;
@NotNull
private String contact;
@NotNull
private String cnic;
@NotNull
private String referred_by;
public Patient(String name, String gender, String specimen, String contact, String cnic, String referred_by) {
this.name = name;
this.gender = gender;
this.specimen = specimen;
this.contact = contact;
this.cnic = cnic;
this.referred_by = referred_by;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Patient patient = (Patient) o;
return reg != null && Objects.equals(reg, patient.reg);
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}
simply used a custom constructor excluding id field and it works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Michael Piefel |
| Solution 3 | csenga |
| Solution 4 | khizar khan |
