i am using hibernate to create entities and database tables
and i am confused about the naming conventions:
let’s give an example:
we have a department entity as follows:
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);
}
what is the best when naming for example the department admin field, do you name the column as department_admin_id or admin_id and what about the case (lower/capital) ?
and when naming the property, what do you name it, departmentAdminId or adminId ?
please advise, what is the best practice in such case, thanks.
If your table is called
departmentthen there’s not much benefit in all it’s columns beginning withdepartment_. Just call themid,name,admin_id, etc.After all, the members of you’re class aren’t prefixed in this way are they? The columns already have an implicit relation to
departmentby being in thedepartmenttable. Further explanation is over verbose and not needed.Edit:
In java classes I would use camel case. For the column name I would seperate with underscores. Most DBs are case insensitive with table and column names.