这可能吗?没有看到太多关于它的讨论。
问问题
19424 次
4 回答
28
当然!根据我的经验,它非常有效。这是一个示例实体:
@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;
}
于 2011-08-01T20:12:59.423 回答
15
您也可以将它与@Data 一起使用(它可以工作!)
@Entity
@Data
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
}
于 2014-10-24T15:52:44.097 回答
10
我从未尝试过使用 Hibernate 的 Lombok,但我不明白为什么它不应该工作。另外,看看这里:http ://groups.google.com/group/project-lombok/browse_thread/thread/294bd52d9d8695df/7bc6b0f343831af1?lnk=gst&q=hibernate#7bc6b0f343831af1
此外,Lombok 项目发布说明明确提到了 Hibernate。
于 2011-02-01T19:06:54.260 回答
4
一个简单的例子;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);
}
}
并且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;
}
于 2019-07-25T06:21:08.637 回答