Wednesday, 15 April 2015

How to use an OR condition in my join for hibernate? -



How to use an OR condition in my join for hibernate? -

how can next sql represented in hibernate?

select * table1 inner bring together table2 on table1.column1 = table2.column1 or table1.column1 = table2.column2 or table1.column1 = table2.column3

table1 has value can exist in either column1, column2 or column3 of table2. need relationship because i'm embedding table2 object in table1's class.

e.g.,

@manytoone @joincolumns({ @joincolumn(name="column1", referencedcolumnname="column1"), @joincolumn(name="column1", referencedcolumnname="column2") }) private table2 object;

the above code not work , throws next error:

caused by: org.hibernate.mappingexception: property mapping has wrong number of columns:

so solution take add together table2 3 separate objects joining on 3 different columns. set optional = true allow left outer join. wasn't crazy approach because seems should done differently.

@manytoone(optional = true) @joincolumn(name="column1", referencedcolumnname="column1") private table2 object1; @manytoone(optional = true) @joincolumn(name="column1", referencedcolumnname="column2") private table2 object2; @manytoone(optional = true) @joincolumn(name="column1", referencedcolumnname="column3") private table2 object3;

hibernate

No comments:

Post a Comment