英文:
Java Quarkus, Create Generic CrudService
问题
我想为自己创建一个Java Quarkus项目,除了资源以外,还要创建通用类,这样我就不必多次实现服务和存储库,只需指定实体类并选择“标准”实现。我的错误是当我想将对象添加到数据库时,出现异常,错误消息为:“IllegalArgumentException: 'Not an Entity' on TestEntity”。
@ApplicationScoped
@Path("test")
@Tag(name = "Tests")
@Consumes("application/json")
@Produces("application/json")
public class TestResource {
@Inject
private TestService testService;
@POST
public Response create(TestEntity te){
Optional<TestEntity> t = testService.create(te);
return Response.status(Response.Status.OK).entity(t.get()).build();
}
}
@RequestScoped
@Transactional
public interface IBaseService<TKey, TEntity> {
public Optional<TEntity> create(TEntity entity);
}
@RequestScoped
@Transactional
public abstract class BaseService<TKey, TEntity> implements IBaseService<TKey, TEntity>{
@Inject IBaseRepository<TKey, TEntity> repository;
public BaseService(){
}
public Optional<TEntity> create(TEntity e) {
Optional<TEntity> result = repository.create(e);
if(result.isPresent()){
return result;
}
return Optional.empty();
}
}
@ApplicationScoped
public class TestService extends BaseService<UUID, TestEntity> {
}
public class TestEntity {
public String s;
public int i;
public Object o;
}
@ApplicationScoped
public interface IBaseRepository<TKey, TEntity> {
public Optional<TEntity> create(TEntity entity);
}
@ApplicationScoped
public interface ITestRepository extends IBaseRepository<UUID, TestEntity> {
}
@ApplicationScoped
public abstract class BaseRepository<TKey, TEntity, TEntityDB> implements IBaseRepository<TKey, TEntity>, PanacheRepositoryBase<TEntity, TKey>{
public BaseRepository(){
}
@Override
public Optional<TEntity> create(TEntity entity) {
if(entity == null){
return Optional.empty();
}
this.persist(entity);
return Optional.of(entity);
}
}
@ApplicationScoped
public class TestRepository extends BaseRepository<UUID, TestEntity, TestEntity> {
}
英文:
I want to create a java quarkus project by creating generic classes for myself except for the resource, so I don't have to implement the service and repository multiple times, just specify the entity classes and choose "standard" implementations. My error is that I get an exception when I want to add an object to the database, error: IllegalArgumentException: "Not an Entity" on TestEntity.
@ApplicationScoped
@Path("test")
@Tag(name = "Tests")
@Consumes("application/json")
@Produces("application/json")
public class TestResource{
@Inject
private TestService testService;
@POST
public Response create(TestEntity te){
Optional<TestEntity> t = testService.create(te);
return Response.status(Response.Status.OK).entity(t.get()).build();
}
}
@RequestScoped
@Transactional
public interface IBaseService<TKey, TEntity> {
public Optional<TEntity> create(TEntity entity);
}
@RequestScoped
@Transactional
public abstract class BaseService<TKey, TEntity> implements IBaseService<TKey, TEntity>{
@Inject IBaseRepository<TKey, TEntity> repository;
public BaseService(){
}
public Optional<TEntity> create(TEntity e) {
Optional<TEntity> result = repository.create(e);
if(result.isPresent()){
return result;
}
return Optional.empty();
}
}
@ApplicationScoped
public class TestService extends BaseService<UUID,TestEntity> {
}
public class TestEntity {
public String s;
public int i;
public Object o;
}
@ApplicationScoped
public interface IBaseRepository<TKey, TEntity> {
public Optional<TEntity> create(TEntity entity);
}
@ApplicationScoped
public interface ITestRepository extends IBaseRepository<UUID,TestEntity> {
}
@ApplicationScoped
public abstract class BaseRepository<TKey, TEntity, TEntityDB> implements IBaseRepository<TKey, TEntity>, PanacheRepositoryBase<TEntity, TKey>{
public BaseRepository(){
}
@Override
public Optional<TEntity> create(TEntity entity) {
if(entity == null){
return Optional.empty();
}
this.persist(entity);
return Optional.of(entity);
}
}
@ApplicationScoped
public class TestRepository extends BaseRepository<UUID, TestEntity,TestEntity> {
}
答案1
得分: 0
TestEntity 不是一个实体。您至少需要使用 @Entity 进行注释。
英文:
TestEntity is not an entity. You need to at least annotate it with @Entity
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论