java 龙目岛与休眠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4866706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:24:09  来源:igfitidea点击:

Lombok with hibernate

javahibernatecode-generationlombok

提问by javaguy

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

这可能吗?没有看到太多关于它的讨论。

采纳答案by Luciano Fiandesio

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

我从来没有用 Hibernate 尝试过 Lombok,但我不明白为什么它不应该工作。另外,看看这里:http: //groups.google.com/group/project-lombok/browse_thread/thread/294bd52d9d8695df/7bc6b0f343831af1?lnk=gst&q=hibernate#7bc6b0f343831af1

Also, Lombok project release notesmention Hibernate explicitely.

此外,Lombok 项目发行说明明确提到了 Hibernate。

回答by wmacura

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

回答by Marcin Szymczak

You can use it also with @Data (and it works !)

您也可以将它与@Data 一起使用(它可以工作!)

@Entity
@Data
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String firstName;
    private String lastName;

}

回答by vault

A simple example; Library.java:

一个简单的例子;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:

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