我正在使用 hibernate 来创建实体和数据库表 我对命名约定感到困惑:
举个例子:
我们有一个部门实体如下:
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "department_id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
private long id;
@NotBlank(message = "{name.required}")
@Size(max = 25, message = "{long.value}")
@Column(name = "department_name", length = 25, nullable = false)
private String name;
@Column(name = "department_admin_id", nullable = true)
private Integer adminId;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "ik_parent_department_id")
// set the generated column name
private Department parentDepartment;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "department_id")
private Set<Employee> employees = new HashSet<Employee>(0);
}
什么是最好的命名,例如 department admin 字段,您将 column 命名为 department_admin_id 还是 admin_id 那么 case(小写/大写)呢?
在命名property 时,您将其命名为departmentAdminId 还是adminId?
请告知,在这种情况下,最佳做法是什么,谢谢。
最佳答案
如果您的表名为 department
,那么所有以 department_
开头的列都没有太大好处。只需将它们命名为 id
、name
、admin_id
等
毕竟,您所在类(class)的成员没有以这种方式作为前缀,对吗?通过在 department
表中,这些列已经与 department
具有隐式关系。进一步的解释过于冗长,不需要。
编辑:
在 java 类中,我会使用驼峰式大小写。对于列名,我会用下划线分隔。大多数数据库对表名和列名不区分大小写。
关于java - 实体字段和数据库表列命名约定的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8120777/