0

I am using Spring Boot 2 with thymeleaf to display data from an existing Oracle 12 database.

The workflow entity is one of my main entities which I would like to display:

@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 holds some information about the workflow like the type of the workflow:

@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;
    }

I have a custom query for the workflow category, as only some information is important for me. I need to decode the category name as I don't have access to the table where the information is stored.

@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();

}

If I am trying to access the data I get the following error message.

More than one row with the given identifier was found: 41, for class: de.elementity.runbook.workflow.WorkflowCategory

The table contains more information per workflow but only the ones from my query are important for me. How can I specify a query for the @OneToOne mapping?

I also tried to use a @ManyToMany mapping, but there I get the following error message:

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]")

This also happens when I remove the DECODE part from the query.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
dni0911
  • 1
  • 1

1 Answers1

0

Can you try with the following attributes on @OneToOne annotation,

cascade = CascadeType.ALL and orphanRemoval = true

raj240
  • 646
  • 7
  • 17
  • Then I am getting ORA-00904: "WORKFLOWCA0_"."NAME": invalid identifier `select workflowca0_.cat_id as cat_id1_11_1_, workflowca0_.name as name2_11_1_, workflowca0_.proc_id as proc_id3_11_1_, workflow1_.proc_id as proc_id1_10_0_, workflow1_.description as description2_10_0_, workflow1_.name as name3_10_0_, workflow1_.proc_ref as proc_ref4_10_0_ from avuser.V_AV_WFPROC_CATEGORIES workflowca0_ left outer join avuser.V_AV_WFPROC workflow1_ on workflowca0_.proc_id=workflow1_.proc_id where workflowca0_.proc_id=?` – dni0911 Feb 11 '20 at 10:41
  • And without the name attribute the same error as above. – dni0911 Feb 11 '20 at 10:45
  • That means you are querying about something which doesnot exists in the table. Cross check the columns once. – raj240 Apr 25 '20 at 05:05
  • Yes, it is only available in the `@Query` I created in the WorkflowCategoryRepository and not in the table. Is there a way to set the custom query to be used for the mapping? I solved my issue for now by creating a custom view, but I sill would like to know how mappings are done with `@Query`. – dni0911 Apr 26 '20 at 08:00