0

我正在使用带有 thymeleaf 的 Spring Boot 2 来显示来自现有 Oracle 12 数据库的数据。

工作流实体是我想展示的主要实体之一:

@Entity
@Immutable
@Table(name = "V_AV_WFPROC", schema="avuser")
public class Workflow {

    @Id
    @Column(name = "proc_id")
    private Long id;
    private String name;
    @Column(name = "proc_ref")
    private String procRef;
    private String description;
    @OneToOne(mappedBy = "workflow")
    private WorkflowCategory workflowCategory;

    public Long getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getProcRef() {
        return procRef;
    }
    public String getDescription() {
        return description;
    }

    public WorkflowCategory getWorkflowCategory() {
        return workflowCategory;
    }

}

workflowCategory保存有关工作流的一些信息,例如工作流的类型:

@Entity
@Immutable
@Table(name = "V_AV_WFPROC_CATEGORIES", schema = "avuser")
public class WorkflowCategory {

    @Id
    @Column(name = "cat_id")
    private Long id;
    private String name;
    @OneToOne
    @JoinColumn(name="proc_id", referencedColumnName = "proc_id")
    private Workflow workflow;

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Workflow getWorkflow() {
        return workflow;
    }

我有一个工作流类别的自定义查询,因为只有一些信息对我很重要。我需要解码类别名称,因为我无权访问存储信息的表。

@Repository
public interface WorkflowCategoryRepository extends CrudRepository<WorkflowCategory, String>{

    @Query(value = "SELECT PROC_ID || CAT_ID AS CAT_ID, PROC_ID, DECODE(CAT_ID, 4, 'Request Workflow', 5, 'Approval Workflow', 6, 'Escalation Workflow', 7, 'Fulfillment Workflow', 51, 'Custom Tasks Workflow', '?') AS NAME FROM WorkflowCategory WHERE CAT_ID IN (4, 5, 6, 7, 51)",
            nativeQuery = true)
    public Iterable<WorkflowCategory> findAll();

}

如果我尝试访问数据,则会收到以下错误消息。

发现多于一行具有给定标识符:41,对于类:de.elementity.runbook.workflow.WorkflowCategory

该表包含每个工作流程的更多信息,但只有我的查询中的信息对我很重要。如何为@OneToOne映射指定查询?

我也尝试使用@ManyToMany映射,但我收到以下错误消息:

2020-02-10 14:48:14.057 DEBUG 35572 --- [nio-8080-exec-1] org.hibernate.SQL                        : select wfcats0_.workflows_proc_id as workflows_proc_id2_12_0_, wfcats0_.wfCats_cat_id as wfCats_cat_id1_12_0_, wfcat1_.cat_id as cat_id1_11_1_, wfcat1_.name as name2_11_1_ from V_AV_WFPROC_CATEGORIES_V_AV_WFPROC wfcats0_ inner join avuser.V_AV_WFPROC_CATEGORIES wfcat1_ on wfcats0_.wfCats_cat_id=wfcat1_.cat_id where wfcats0_.workflows_proc_id=?
2020-02-10 14:48:14.221  WARN 35572 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 972, SQLState: 42000
2020-02-10 14:48:14.221 ERROR 35572 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ORA-00972: idnetifier too long

2020-02-10 14:48:14.229 ERROR 35572 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "workflows": An error happened during template parsing (template: "class path resource [templates/workflows.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/workflows.html]")

当我DECODE从查询中删除部分时也会发生这种情况。

4

1 回答 1

0

您可以尝试在 @OneToOne 注释上使用以下属性,

级联 = CascadeType.ALL 和 orphanRemoval = true

于 2020-02-11T10:27:41.417 回答