lepdou
lepdou

Reputation: 27

Is there any anotation to make a Spring data repositories method names shorter?

The method names from Spring data repositories may become really long. For example:

List<InstanceConfig> findByConfigAppIdAndConfigClusterNameAndConfigNamespaceNameAndDataChangeLastModifiedTimeAfter(
      String appId, String clusterName,
      String namespaceName, Date validDate);

I am looking for any annotation that would allow me to do something like this:

@XX(key = "findByConfigAppIdAndConfigClusterNameAndConfigNamespaceNameAndDataChangeLastModifiedTimeAfter")
List<InstanceConfig> customMethodName(String appId, String clusterName,
      String namespaceName, Date validDate);

Is there any solution to the name length problem?

Upvotes: 1

Views: 570

Answers (1)

Pau
Pau

Reputation: 16116

With java 8 you can create a default method and call the method with the too length name:

List<InstanceConfig> findByConfigAppIdAndConfigClusterNameAndConfigNamespaceNameAndDataChangeLastModifiedTimeAfter(String appId, String clusterName,
      String namespaceName, Date validDate);

default List<InstanceConfig> customMethodName(String appId, String clusterName,
      String namespaceName, Date validDate) {
   return findByConfigAppIdAndConfigClusterNameAndConfigNamespaceNameAndDataChangeLastModifiedTimeAfter(appId,clusterName,
     namespaceName, validDate);
}

Then if you call customMethodName is as you are calling findByConfigAppIdAndConfigClusterNameAndConfigNamespaceNameAndDataChangeLastModifiedTimeAfter.

Upvotes: 1

Related Questions