英文:
Spring boot, storing CRUD operations in abstract service
问题
我有两个 JPA 实体,如下所示:
@Entity
@Table(name = "A")
public class A {...}
@Entity
@Table(name = "B")
public class B {...}
还有以下服务:
@Service
public class AServiceImpl implements AService {
@Autowired
private ARepository aRepository;
public void create() {...}
public void update(long id) {...}
public void doA() {...}
}
@Service
public class BServiceImpl implements BService {
@Autowired
private BRepository bRepository;
public void create() {...}
public void update(long id) {...}
public void doB() {...}
}
因此,在方法create
和update
中,我有仅由仓库不同的代码。我想将 CRUD 方法存储在其他一些基本服务中,但实际上不太理解如何管理仓库继承。有任何帮助,谢谢。
英文:
I have two JPA entites like
@Entity
@Table(name = "A")
public class A {...}
@Entity
@Table(name = "B")
public class B {...}
And services like
@Service
public class AServiceImpl implements AService {
@Autowired
private ARepository aRepository;
public void create() {...}
public void update(long id) {...}
public void doA() {...}
}
@Service
public class BServiceImpl implements BService {
@Autowired
private BRepository bRepository;
public void create() {...}
public void update(long id) {...}
public void doB() {...}
}
So, in methods create
and update
I have code what differs only by repository. I would like to store crud methods in some other, basic service, but do not really understand how could I manage repository inheritance.
Any help, please.
答案1
得分: 2
I think you can use Genarics over here.
public class RepositoryService<R extends JpaRepository<T, Long>, T> {
@Autowired R repository;
public void insert(T object) {
repository.save(object);
}
}
How to effectively use these repositories, that details you can refer to this Baeldung article - https://www.baeldung.com/spring-autowire-generics
Update: This is an alternate approach you can try out
public abstract class RepositoryService<T>{
abstract Class getRepositoryClass();
Map<Class, GenericRepository> repositories;
@Autowired
List<GenericRepository> repositoriesList;
@PostConstruct
public void setupMap() {
// Convert List into Map, with the class of Object to be saved.
}
public void insert(T object) {
repositories.get(object.getClass()).save(object);
}
}
英文:
IMP: This code is compiling, I havent tested it.
I think you can use Genarics over here.
public class RepositoryService <R extends JpaRepository<T, Long>, T> {
@Autowired R repository;
public void insert(T object) {
repository.save(object);
}
}
How to effectively use these reporitories, that details you can refer to this baeldung article - https://www.baeldung.com/spring-autowire-generics
Update: This is an alternate approach you can try out
public abstract class RepositoryService <T>{
abstract Class getRepositoryClass();
Map<Class, GenericRepository> repositories;
@Autowired
List<GenericRepository> repositoriesList;
@PostConstruct
public void setupMap() {
// Convert List into Map, with the class of Objest to be saved.
}
public void insert(T object) {
repositories.get(object.getClass()).save(object);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论