36

Is this possible? Haven't seen much discussion on it.

4 Answers 4

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;
}
6
  • 15
    If you use Data, you don't have to put Getter and Setter on each field. You might want to also specify EqualsAndHashcode(of={"id"}) Dec 21, 2011 at 9: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 5, 2013 at 13:57
  • 2
    That'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! Jul 4, 2014 at 20:07
  • @qwertzguy : Could you please elaborate further on your reply stated above. I am missing your point.
    – ecdhe
    Dec 1, 2017 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 1, 2017 at 22:41
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;

}
2
  • 9
    I 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, 2015 at 12:18
  • 1
    in fact it doesn't! This article explains why in details jpa-buddy.com/blog/lombok-and-jpa-what-may-go-wrong. Jul 11, 2021 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.

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.