0

Good day, I'm learning spring. I see several options for working with the entity. I can't understand if there is any difference between these 2 methods.

First

@Entity
@NoArgsConstructor
@EqualsAndHashCode    
public class ProductTest {
private Long idProduct;
private String nameProduct;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_product", table = "product", nullable = false)
public Long getIdProduct() {
    return idProduct;
}

public void setIdProduct(Long idProduct) {
    this.idProduct = idProduct;
}

@Basic
@Column(name = "name_product", table = "product", nullable = false, length = 100)
public String getNameProduct() {
    return nameProduct;
}

public void setNameProduct(String nameProduct) {
    this.nameProduct = nameProduct;
    }
}

Second

@Entity
@NoArgsConstructor
@EqualsAndHashCode
public class ProductTest {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_product", table = "product", nullable = false)
    private Long idProduct;
    @Basic
    @Column(name = "name_product", table = "product", nullable = false, length = 100)
    private String nameProduct;

    public Long getIdProduct() {
        return idProduct;
    }

    public void setIdProduct(Long idProduct) {
        this.idProduct = idProduct;
    }

    public String getNameProduct() {
        return nameProduct;
    }

    public void setNameProduct(String nameProduct) {
        this.nameProduct = nameProduct;
    }
}

What would be better for working with the database?

Also interested in whether I can add columns to tables via Entity and how? I will be glad to hear your advice.

WBLord
  • 874
  • 6
  • 29

1 Answers1

0

The main difference is the separation of logic. If logic is not needed in the getter and setter, then the annotation is placed above the fields, and getters and setters can be removed from the Lombok annotation. For more information, follow the links below Hibernate Annotations - Which is better, field or property access? Hibernate Annotation Placement Question

WBLord
  • 874
  • 6
  • 29