Hibernation criterion without binding tables
I have 2 tables:
Table 1 and Table 2
Now Table1 has 3 columns: t1, t2, t3 and Table2 has 2 columns t4 and t5.
I need to get data from both tables by joining, but there is no mapped relationship between the two tables in annotation or xml.
Now the main problem is that I have to use the hibernation prediction to extract the selected columns from both tables: t1, t2 from Table 1 and t4 from Table 2.
I went through the internet but found examples of tables with an association.
Glad if there are any recommendations on this matter.
source to share
Yes, this one is supported in Hibernate. Here's the only thing we have to use HQL :
- 16.2. Suggestion from paragraph (cite :)
Multiple classes may appear, resulting in a Cartesian product or cross join.
from Formula, Parameter
from Formula as form, Parameter as param
So, in our case:
session
.createQuery("SELECT t1.prop1, t1.prop2, t1.prop3 "
+ " t2.prop4, t2.prop5 "
+ " FROM Entity1 AS t1, Entity2 As t2 " +
// if there is some relation - unmapped
+ " WHERE t1.someProperty = t2.someProperty "
+ " AND ... "
)
.setMaxResults(10) // we can even page here
.list()
NOTE. I used prop1, prop2 and Entity1, Entity2 ... to make it feel like it's HQL. We are talking about displayables, not tables or columns.
And we will get a collection of an array object[]
...
source to share