Is this possible? Haven't seen much discussion on it.
Asked
Active
Viewed 2.1k times
4 Answers
29
Sure! It works great from my experience. Here's an example entity:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class PingerEntity {
// ID
@Id
@Getter
@Setter
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// USER
@Getter
@Setter
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private UserEntity user;
// URL
@Getter
@Setter
@Basic(optional = false)
private String url;
/**
* The number of seconds between checks
*/
@Getter
@Setter
@Basic(optional = false)
private int frequency;
@Getter
@Setter
@Basic(optional = false)
@Enumerated(EnumType.STRING)
public MonitorType monitorType;
}

wmacura
- 799
- 8
- 12
-
15If you use Data, you don't have to put Getter and Setter on each field. You might want to also specify EqualsAndHashcode(of={"id"}) – Roel Spilker Dec 21 '11 at 09:20
-
26@RoelSpilker Yes but be careful with entities having OneToMany and ManyToOne, because the ToString of lombok will make an infinite loop. – qwertzguy Mar 05 '13 at 13:57
-
2That's the one I was looking for, I had an infinite loop for one of these entities and it caused a stackoverflowerror. Thanks for the tip! – Dieter Hubau Jul 04 '14 at 20:07
-
@qwertzguy : Could you please elaborate further on your reply stated above. I am missing your point. – ecdhe Dec 01 '17 at 17:09
-
2@ecdhe basically having an entity that links to the other and itself links back to the first one. They will then each call toString on each other repeatedly. – qwertzguy Dec 01 '17 at 22:41
-
@qwertzguy now I understand it much better. Many thanks :) – ecdhe Dec 01 '17 at 23:33
16
You can use it also with @Data (and it works !)
@Entity
@Data
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
}

Marcin Szymczak
- 11,199
- 5
- 55
- 63
-
9I had problems with @Data annotation and hibernate. First problme was with hashCode() generated method, hibernate just failed to create entitry. Other one with toString() method in case model cantains Lists or Sets. Just #Setter #Getter so far so good. #ToString only if model conatins only plain data fields – ekitru Mar 23 '15 at 12:18
-
1in fact it doesn't! This article explains why in details https://www.jpa-buddy.com/blog/lombok-and-jpa-what-may-go-wrong/. – aleksey.stukalov Jul 11 '21 at 20:22
10
I have never tried Lombok with Hibernate but I don't see why it shouldn't work. Also, take a look here: http://groups.google.com/group/project-lombok/browse_thread/thread/294bd52d9d8695df/7bc6b0f343831af1?lnk=gst&q=hibernate#7bc6b0f343831af1
Also, Lombok project release notes mention Hibernate explicitely.

Luciano Fiandesio
- 10,037
- 10
- 48
- 56
4
A simple example; Library.java
:
@Data
@NoArgsConstructor // JPA
@Entity
@Table(name = "libraries")
public class Library {
@Id
@GeneratedValue
private Long id;
@OneToMany(cascade = CascadeType.ALL)
@EqualsAndHashCode.Exclude
// This will be included in the json
private List<Book> books = new ArrayList<>();
@JsonIgnore
public void addBook(Book book) {
books.add(book);
book.setLibrary(this);
}
}
And Book.java
:
@Data
@NoArgsConstructor // JPA
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue
private Long id;
@NotBlank
private String title;
@ManyToOne
@JoinColumn(name = "library_id") // Owning side of the relationship
@EqualsAndHashCode.Exclude
@JsonIgnore // Avoid infinite loops
private Library library;
}

vault
- 3,930
- 1
- 35
- 46