Ответ 1
Я не думаю, что есть способ предоставить общий класс для аннотации. Это должна быть постоянная времени компиляции.
Но как насчет этого? (много кода, полный пример).
Spring Base
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
юридические лица
@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;
}
Контроллеры
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);
}
}
Хранилища
@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> {
}
Он работает и абстрагирует как можно больше. Вам все равно придется порождать дополнительные @Controller
и @Repository
на каждый @Entity
. Хотя в основном они будут пустыми заглушками. Но я не думаю, что вы можете полностью обмануть @Querydsl или Spring Data для правильного использования обобщений в union.