Can Hibernate return a null object when using createSQLQuery () to join tables?

I have three tables / classes that I retrieve using left outer join. I execute the following query:

List<Object[]> l = session.createSQLQuery("SELECT a.*, b.*, c.* " + " FROM (table_a a INNER JOIN table_b b ON a.some_id = b.some_id) " + " LEFT OUTER JOIN table_c c ON c.some_id = a.some_id" ).addEntity("a", A.class).addEntity("b", B.class).addEntity("c", C.class).list(); 

The request fails if the rows in A and B exist, but in C there is no corresponding row, since hibernate is trying to assign null values ​​to non-zero attributes in C (I cannot change these attributes as nullable). Is there a parameter in sleep mode for returning a null object for C if a line in C does not exist and is not trying to create an object with null values?

+4
source share
1 answer

if I did not understand correctly. you are trying to get the result if c is null or not null.

you can add this to the where clause as follows:

 and ((c.name=1234 and c.price=1234) or c.id is null) 
0
source

Source: https://habr.com/ru/post/1389554/


All Articles