4

It is well known that Hibernate needs no-args constructor, but I tried to delete it and surprisingly everything work fine. I use Spring-Boot in my project and while I run the program I got this message:

2020-09-04 00:13:24.749  INFO 12825 --- [         task-1] org.hibernate.tuple.PojoInstantiator     : HHH000182: No default (no-argument) constructor for class: com.jb.couponSystem.data.entity.Coupon (class must be instantiated by Interceptor)

What is that Interceptor that instantiated my class? and why in other projects if I don't have no args constructor Hibernate throws an exception (as expected)?

This is the class, I use Lombok @Data for reducing boilerplate code:

@Entity
@Data
public class Coupon {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.NONE)
private long id;
@Setter(AccessLevel.NONE)
@Column(nullable = false)
private  String title;
@Setter(AccessLevel.NONE)
@Column(nullable = false)
private LocalDate startDate;
@Setter(AccessLevel.NONE)
@Column(nullable = false)
private LocalDate endDate;
@Setter(AccessLevel.NONE)
@Column(nullable = false)
private Category category;
private int amount;
@Setter(AccessLevel.NONE)
@Column(nullable = false)
private String description;
@Setter(AccessLevel.NONE)
@Column(nullable = false)
private double price;
@Setter(AccessLevel.NONE)
private String imageURL;
@ManyToOne
@JoinColumn(name = "company_id")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Company company;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@ManyToMany
@JoinTable(name = "customer_coupon",
        joinColumns = @JoinColumn(name = "coupon_id"),
        inverseJoinColumns = @JoinColumn(name = "customer_id"),
        uniqueConstraints = @UniqueConstraint(columnNames
                = {"customer_id", "coupon_id"}))
private List<Customer> customers;


public Coupon(String title, LocalDate startDate, LocalDate endDate,
              Category category, int amount, String description, double price, String imageURL) {
    this.title = title;
    this.startDate = startDate;
    this.endDate = endDate;
    this.category = category;
    this.amount = amount;
    this.description = description;
    this.price = price;
    this.imageURL = imageURL;
}


public boolean isOutOfStock() {
    return amount <= 0;
}

}

Update: To make clear, this behavior for sure is not related to lombok @Data . Even when I delete that annotation and add getters and setters it still works with the same Hibernate message.

here is some interesting answer concerning this issue, but it assumes that You have to implement yourself Interceptor- what I didn't do. Maybe Spring-Boot itself passes an already implemented interceptor while creating the session factory?

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • 1
    Does this answer your question? [Why does Hibernate require no argument constructor?](https://stackoverflow.com/questions/2935826/why-does-hibernate-require-no-argument-constructor). Especially this answer: https://stackoverflow.com/a/29433238/4880379 ? – kasptom Sep 04 '20 at 07:38
  • 2
    @kasptom That is interesting answer but it assumes that You implemented instantiate() method of Interceptor. In my case I didn't do that. Does Spring-Boot passes an already implemented interceptor while creating the Session factory ? – Itzhak Bukinski Sep 04 '20 at 08:36

0 Answers0