英文:
What is the difference between ways to get from the database via annotation spring?
问题
以下是翻译好的部分:
第一种方法:
@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;
}
}
第二种方法:
@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;
}
}
关于与数据库的交互方式,这两种方法在本质上是相同的,只是代码的排列方式有所不同。您可以根据个人偏好选择其中之一。通过使用@Column
注解,您可以将字段映射到数据库表的列,通过@Table
注解,您也可以为实体指定表名。
如果您想要在实体类中添加新的字段并映射到数据库表的列,您只需在类中添加新的成员变量,并使用@Column
注解来指定映射的列名、数据类型等信息。
希望对您有所帮助,如果您有任何疑问,请随时提问。
英文:
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.
答案1
得分: 0
主要区别是逻辑的分离。
如果在 getter 和 setter 中不需要逻辑,则注解会放置在字段之上,getter 和 setter 可以从 Lombok 注解中移除。
欲了解更多信息,请查看以下链接:
https://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access
https://stackoverflow.com/questions/305880/hibernate-annotation-placement-question
英文:
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
https://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access
https://stackoverflow.com/questions/305880/hibernate-annotation-placement-question
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论