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?
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*
orfind*
.add*
can be applied onINSERT
operations, likeaddPhoneNumber(Long userId)
.get*
can be applied forSELECT
operations, likegetEmailAddress(Long userId)
.set*
can be applied on method that performs anUPDATE
operation.delete*
can be applied onDELETE
operations, likedeleteUser(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 exampleisUsernameAvailable(String username)
.I am aware of conventions like the following:
methods starting with
find
performselect
operations, and method names containing the search criteria, likefindById
,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.