开发者

Lombok with hibernate

Is this possible? Haven't seen much discu开发者_开发技巧ssion on it.


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


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;

}


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜