Question

Est-ce possible? Je n'ai pas vu beaucoup de discussions là-dessus.

Était-ce utile?

La solution

Je ne l'ai jamais essayé Lombok avec Hibernate, mais je ne vois pas pourquoi il ne devrait pas fonctionner. De plus, jetez un coup d'oeil ici: http://groups.google.com/group/project-lombok/browse_thread/thread/294bd52d9d8695df/7bc6b0f343831af1?lnk=gst&q=hibernate#7bc6b0f343831af1

En outre, Lombok notes de version du projet mention Hibernate explicitement.

Autres conseils

Bien sûr! Il fonctionne très bien de mon expérience. Voici une entité exemple:

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

Vous pouvez l'utiliser aussi avec @data (et ça marche!)

@Entity
@Data
public class Customer {

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

}

Un exemple simple: 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);
  }
}

Et 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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top