简体   繁体   中英

Springboot + JPA : List all fieldnames for each fieldtype

I am using Spring Boot + JPA for my project. I want a fieldName list for each type from my table. So I did this by using query with named parameters using JPQL . I made two methods in the repository.

  1. First Method:
    This method is for getting distinct list of type .
      @Query(value = "SELECT DISTINCT fc.type FROM FieldCapabilityModel fc")
      List<String> findDistinctType();
  1. Second Method:
    This method is for getting a list of names for particular type .
     @Query("SELECT fc.fieldName FROM FieldCapabilityModel fc where fc.type=:type")
     List<String> findByType(@Param("type) String type);

This is my service component.

@Service
public class FieldCapabilityService {

    @Autowired
    FieldCapabilityRepo fcRepo;

    public List<Map<String, Object>> getAllFieldAndType() { 
                // First Method is called here  
        return fcRepo.findDistinctType().stream().map(type -> {
                Map<String, Object> resultMap = new HashMap<String, Object>();
                resultMap.put("type", type);
                // Second Method is called here
                resultMap.put("field", fcRepo.findByType(type));
                return resultMap;
            }).collect(Collectors.toList());            
    }
}

When I called using the rest controller my API response is:

{
  "code": 200,
  "message": "Data Fetched",
  "data": [
    {
      "field": [
        "name.lname",
        "city",
        "name.fname",
        "name",
        "id"
      ],
      "type": "text"
    },
    {
      "field": [
        "_ignored"
      ],
      "type": "_ignored"
    },
    {
      "field": [
        "_seq_no"
      ],
      "type": "_seq_no"
    }
  ]
}

I am getting the exact output as I want and also this is a simple data response I have more than 16 types. Albeit I want to know that this is an efficient and robust way.
My questions are:

Can we use any other queries for this?
Can we use @NamedQuery instead of this if yes then why?
What should be the reduced, robust, and efficient method for this execution?

Please give me suggestions if I want to improve anything into this solution or propose a new solution if that is better than this solution.

You can fetch all type & fieldName in one query

@Query(value = "SELECT NEW org.apache.commons.math3.util.Pair(fc.type as type, fc.fieldName as name) FROM FieldCapabilityModel fc")
  public List<Pair<Integer, Integer>> findAllTypeWithName();

And then summarize data. Create a map of Type where Key is fieldNames

Map<String, List<String>> fieldNameByType = fcRepo.findAllTypeWithName()
        .stream()
        .collect(Collectors.groupingBy(Pair::getKey, 
                Collectors.mapping(Pair::getValue, Collectors.toList())));

Then you can prepare your response using this map.

return fieldNameByType.entrySet().stream().map(e -> {
                Map<String, Object> resultMap = new HashMap<String, Object>();
                resultMap.put("type", e.getKey());
                resultMap.put("field", e.getValue());
                return resultMap;
            }).collect(Collectors.toList()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM