Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QuerydslPredicate and generic class

I have many controllers with same methods, difference is only entity class.

I want to create generic BaseController and having problem with QuerydslPredicate annotaion (cannot set root, this is generic)

class abstract BaseController<T extends BaseEntity> {
   @Autowired
   private Repository<T, Long> repository;

   @RequestMapping(method = RequestMethod.GET)
   public Page<T> findAll(@QuerydslPredicate Predicate predicate, Pageable pageable) {
      return repository.findAll(predicate, pageable);
   }
}

Method extractTypeInfo from QuerydslPredicateArgumentResolver return T. But needed Entity class.

And I cannot set root value to QuerydslPredicate(don't have class)

@QuerydslPredicate(root = T.class)

Any help on how to achieve this?

like image 750
Yura Heineken Avatar asked May 27 '16 11:05

Yura Heineken


1 Answers

I don't think there's way to provide the generic class to annotation. It should be compile-time constant.

But how about this? (a lot of code, full example).

Spring Base

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Entities

@MappedSuperclass
@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class BaseEntity {

    @Id
    @Column(name = "id")
    private Integer id;

    @Column(name = "created_at")
    private Date createdAt;
}

@Entity
@Table(name = "user")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class User extends BaseEntity {

    @Column(name = "username")
    private String userName;
}

@Entity
@Table(name = "another_user")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AnotherUser extends BaseEntity {

    @Column(name = "another_username")
    private String anotherUserName;
}

Controllers

public abstract class BaseEntityController<T extends BaseEntity> {

    private final BaseEntityRepository<T> repository;

    @Autowired
    protected BaseEntityController(BaseEntityRepository<T> repository) {
        this.repository = repository;
    }

    @GetMapping(value = "/index")
    public Page<T> index(Predicate predicate, Pageable pageable) {
        return repository.findAll(predicate, pageable);
    }
}

@RestController
@RequestMapping("/user")
public class UserController extends BaseEntityController<User> {

    protected UserController(BaseEntityRepository<User> repository) {
        super(repository);
    }

    @Override
    public Page<User> index(@QuerydslPredicate(root = User.class) Predicate predicate, Pageable pageable) {
        return super.index(predicate, pageable);
    }
}

@RestController
@RequestMapping("/anotheruser")
public class AnotherUserController extends BaseEntityController<User> {

    protected AnotherUserController(BaseEntityRepository<User> repository) {
        super(repository);
    }

    @Override
    public Page<User> index(@QuerydslPredicate(root = AnotherUser.class) Predicate predicate, Pageable pageable) {
        return super.index(predicate, pageable);
    }
}

Repositories

@NoRepositoryBean
public interface BaseEntityRepository<T extends BaseEntity> extends CrudRepository<T, Integer>, QuerydslPredicateExecutor<T> {
}

@Repository
public interface UserEntityRepository extends BaseEntityRepository<User> {
}

@Repository
public interface AnotherUserEntityRepository extends BaseEntityRepository<AnotherUser> {
}

It works and abstracts as much as possible. You still will have to spawn extra @Controller and @Repository class per @Entity though. Although they will be mostly empty stubs. But I don't think you can completely trick either @Querydsl or Spring Data to use generics properly in union.

like image 87
Mikhail Kholodkov Avatar answered Oct 11 '22 18:10

Mikhail Kholodkov