I have two tables in my PostgreSQL database:
CREATE TABLE tableOne (id int, name varchar(10), address varchar(20))
CREATE TABLE tableTwo (id int, info text, addresses varchar(20)[])
now I want to create a join as follows:
SELECT * FROM tableOne JOIN tableTwo ON address = ANY(addresses)
I tried to achieve this using Hibernate - class TableOne:
@Entity
@Table(name = "tableOne")
class TableOne {
private int id;
private TableTwo tableTwo;
private String address;
@Id
@Column(name = "id")
public getId() { return id; }
@ManyToOne
@JoinFormula(value = "address = any(tableTwo.addresses)",
referencedColumnName = "addresses")
public TableTwo getTableTwo(){
return tableTwo;
}
// Setters follow here
}
But Hibernate keeps generating queries with non-sense JOIN clauses, like:
... JOIN tableTwo ON _this.address = any(tableTwo.addresses) = tableTwo.addresses
How do I tell Hibernate using annotations to format my join query correctly? Unfortunately, our project must be restricted only to the Criteria API.
EDIT:
After suggestion from ashokhein in the comments below, I annotated the method getTableTwo()
with just @ManyToOne
- and now I would like to do the join using Criteria API, presumably with createAlias(associationPath,alias,joinType,withClause)
method where withClause
would be my ON
clause in the join.
But Im not sure what to put as associationPath
and alias
parameters.
Any hints?