Question

Is there a standard naming convention for DAO methods, similar to JavaBeans?

For example, one naming convention I've seen is to use get() to return a single entity and find() to return a List of entities.

If there isn't one, what's the one your team is using and why?

Was it helpful?

Solution 2

Usually I name the methods in such way that the name hints the type of the CRUD operation that will be applied by the method, like add*, save* or find*.

  • add* can be applied on INSERT operations, like addPhoneNumber(Long userId).

  • get* can be applied for SELECT operations, like getEmailAddress(Long userId).

  • set* can be applied on method that performs an UPDATE operation.

  • delete* can be applied on DELETE operations, like deleteUser(Long userId). Althought I'm not pretty sure how useful is the physical delete. Personally, I would set a flag that denotes that the row is not gonna be used, rather than performing a physical delete.

  • is* can be applied on a method that check something, for example isUsernameAvailable(String username).

OTHER TIPS

I am aware of conventions like the following:

  • methods starting with find perform select operations, and method names containing the search criteria, like findById, findByUsername, findByFirstNameAndLastName, etc.

  • modification methods start with create, update, delete.

Check out the conventions used by Spring Data JPA. This is part of the Spring framework that writes the DAOs automatically based on among other things inspection of the method name based on naming conventions.

get() for single entities does not seem to be a good option, as get is associated by Java developers to a Java-bean getter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top