Java Quarkus,创建通用的CrudService

huangapple go评论71阅读模式
英文:

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(&quot;test&quot;)
@Tag(name = &quot;Tests&quot;)
@Consumes(&quot;application/json&quot;)
@Produces(&quot;application/json&quot;)
public class TestResource{
@Inject
private TestService testService;
@POST
public Response create(TestEntity te){
Optional&lt;TestEntity&gt; t = testService.create(te);
return Response.status(Response.Status.OK).entity(t.get()).build();
}
}
@RequestScoped
@Transactional
public interface IBaseService&lt;TKey, TEntity&gt; {
public Optional&lt;TEntity&gt; create(TEntity entity);
}
@RequestScoped
@Transactional
public abstract class BaseService&lt;TKey, TEntity&gt; implements IBaseService&lt;TKey, TEntity&gt;{
@Inject IBaseRepository&lt;TKey, TEntity&gt; repository;
public BaseService(){
}
public Optional&lt;TEntity&gt; create(TEntity e) {
Optional&lt;TEntity&gt; result = repository.create(e);
if(result.isPresent()){
return result;
}
return Optional.empty();
}
}
@ApplicationScoped
public class TestService extends BaseService&lt;UUID,TestEntity&gt; {
}
public class TestEntity {
public String s;
public int i;
public Object o;
}
@ApplicationScoped
public interface IBaseRepository&lt;TKey, TEntity&gt; {
public Optional&lt;TEntity&gt; create(TEntity entity);
}
@ApplicationScoped
public interface ITestRepository extends IBaseRepository&lt;UUID,TestEntity&gt; {
}
@ApplicationScoped
public abstract class BaseRepository&lt;TKey, TEntity, TEntityDB&gt; implements IBaseRepository&lt;TKey, TEntity&gt;, PanacheRepositoryBase&lt;TEntity, TKey&gt;{
public BaseRepository(){
}
@Override
public Optional&lt;TEntity&gt; create(TEntity entity) {
if(entity == null){
return Optional.empty();
}
this.persist(entity);
return Optional.of(entity);
}
}
@ApplicationScoped
public class TestRepository extends BaseRepository&lt;UUID, TestEntity,TestEntity&gt; {
}

答案1

得分: 0

TestEntity 不是一个实体。您至少需要使用 @Entity 进行注释。

英文:

TestEntity is not an entity. You need to at least annotate it with @Entity

huangapple
  • 本文由 发表于 2023年7月6日 15:34:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定