How do I write hql query with cast?

后端 未结 4 584
悲&欢浪女
悲&欢浪女 2020-12-06 09:04

I need to combine 2 tables using hql, both are having common column, but table1 common column is integer and table2 common column is <

相关标签:
4条回答
  • 2020-12-06 09:26

    HQL supports CAST (if underlying database supports it), you can use it:

    select a.id as id,a.name as name,b.address as address 
    from Personal as a,Home as b
    where cast(a.id as string) = b.studid 
    

    See also:

    • 16.10. Expressions
    0 讨论(0)
  • 2020-12-06 09:33

    Just noticed that you are using JPA, there you can not cast or convert datatpes. In the query language, only values of the same type can be compared! read in http://download.oracle.com/javaee/5/tutorial/doc/bnbuf.html#bnbvu

    0 讨论(0)
  • 2020-12-06 09:46

    I had to cast it to String like so :

     @Query( value = "select new com.api.models.DResultStatus("+
                "cast(ds.demoId as java.lang.String),cast(ds.comp as java.lang.String),cast(ds.dc as java.lang.String),cast(be.buildUrl as java.lang.String)")
    
    0 讨论(0)
  • 2020-12-06 09:49

    You really need to think why have you got a need to join two entities by properties of different types. Most likely it suggests that some of the entities need to be refactored, which could include changing data types for columns of the underlying db tables. If the model is correct there will be no need to twist Hibernate.

    0 讨论(0)
提交回复
热议问题