How to get the type of generic T, that is, T.class

Keywords: MongoDB Spring Hibernate

How to get the type of generic T, that is, T.class

  • I want to write a mongodb tool class in spring boot, and simply encapsulate some of mongodb's entries. In the process of writing the tool class, I found that many of the mongodb methods integrated in spring boot need to use entity class types. In the tool class, I use generic T to represent entity classes, so the biggest problem encountered is that I can'T get T.class. I don'T know what it means. Just look at the examples.

  • Questions - Examples

    • Unsightly tools

      /**
       * @author zhao_jianbo
       */
      @Component
      public class MongoDbOperImpl<T> implements BaseDbOper<T> {
      
          @Autowired
          private MongoTemplate mongoTemplate;
      
          /**
           * Delete data by id
           *
           * @param id
           * @param entity
           * @return Number of deleted data
           */
          @Override
          public Long deleteById(String id, T entity) {
              Query query = new Query(Criteria.where("id").is(id));
              return mongoTemplate.remove(query,     entity.getClass()).getDeletedCount();
          }
      
      
    • From the above example, we can see that mongoTemplate.remove() needs a class of entity class, that is, T.class. Many articles about how to obtain T.class have been searched on the Internet, but those methods only apply to the type of T that is clear, such as mongodboperimple < user > so we have no choice but to manually pass in the entity class. Although the flexibility of the code is preserved, it looks very ugly. I just Want to delete a piece of data according to the id, when calling the method, it must pass in the entity class, which is very bad!

  • Improved method

    • The improved method comes from HibernateDaoImpl class

    • I don't know exactly why it works like this. Let's talk about it

    • The improved example is as follows

    • Tool class

      /**
       * @author zhao_jianbo
       */
      @Component
      public class MongoDbSupport<T> implements BaseDbOper<T> {
      
          @Autowired
          private MongoTemplate mongoTemplate;
      
          /**
           * Get T.class
           *
           * @return
           */
          private Class<T> getEntry() {
              Type type = getClass().getGenericSuperclass();
              Class<T> result = null;
              if (type instanceof ParameterizedType) {
                  ParameterizedType pType = (ParameterizedType) type;
                  result = (Class<T>) pType.getActualTypeArguments()[0];
              }
              return result;
          }
      
          /**
           * Delete data by id
           *
           * @param id
           * @return Number of deleted data
           */
          @Override
          public Long deleteById(String id) {
              Query query = new Query(Criteria.where("id").is(id));
              return mongoTemplate.remove(query, getEntry()).getDeletedCount();
          }
      
    • Entity class

      /**
       * @author zhao_jianbo
       */
      @Data
      @NoArgsConstructor
      @AllArgsConstructor
      @Document(collection = "book")
      @Repository
      public class Book extends MongoDbSupport<Book> {
      	......
      }
      

    Now understand why hibernate and mybitas need entity classes and inheritance tool classes

Posted by Jade on Sat, 23 Nov 2019 12:33:40 -0800