0

The Question has been already asked here , but my context is different. I am querying (table name-reports_tbl ,Entity name -Report) to take out reports based on join_column menu_id in above table which joins to (Table name-menu_tbl,Entity name-Menu),using query

List<Report> reports= entityManager.createQuery(
                "SELECT r FROM Report r WHERE r.menu IN :reports_menu_id",Report.class)
                .setParameter("reports_menu_id",menu)
                .getResultList();

In Report Entity, member variable menu is declared like :

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="menu_pk")
protected Menu menu;

In Menu Entity reports is declared like :

@OneToMany(
            fetch=FetchType.LAZY,
            mappedBy="menu",
            cascade=CascadeType.ALL
        )
protected List<Report> reports = new ArrayList<Report>(); 

But i am getting the ABOVE EXCEPTION if i try to pass Menu object to setParameter reports_menu_id.

Menu menu = new Menu();
menu.setMenuId(Long.parseLong("12"));

if i pass 12 to the query generated it returns rows.

select report0_.id as id1_3_,... report0_.table_of_contents as table_o24_3_ 
from reports_tbl report0_ 
where report0_.menu_pk in (?) <-- generated query in hibernate

select report0_.id as id1_3_,...report0_.table_of_contents AS table_o24_3_
FROM
reports_tbl report0_
WHERE
report0_.menu_pk IN (12);<-- executed manually by passing value=12 

Am i missing something. Directly passing string value 12 :

List<Report> reports= entityManager.createQuery(
                "SELECT r FROM Report r WHERE r.menu IN 
:reports_menu_id",Report.class)
                .setParameter("reports_menu_id","12")
                .getResultList();

says Parameter value [12] did not match expected type [com.xyz.entity.Menu (n/a)] .Waiting for any Help.Thanks

niranjan
  • 149
  • 1
  • 8

1 Answers1

0

If you are going to query the Report with the menuId then you can just replace this part in the query WHERE r.menu.menuId IN :reports_menu_id then pass the id directly .. the issue with the above is that you are trying to use an unmanaged entity in the query

Alin Sînpălean
  • 5,239
  • 16
  • 19
osama yaccoub
  • 1,630
  • 1
  • 14
  • 33