I've got two tables
Branches{ id PK lft rgt }
Products{ id PK branches_lft FK }
Code:
@Entity
@Table(name="products")
public class Product implements Serializable{
@Id @GeneratedValue
private Long id;
@Column(name="lft")
private Long left;
@Column(name="rgt")
private Long right;
@Column(nullable=false)
private String name;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="branches_lft",referencedColumnName="lft")
private Branch branch;
public Branch getBranch() {
return branch;
}
public void setBranch(Branch branch) {
this.branch = branch;
}
}
@Entity
@Table(name="product_branches")
public class Branch {
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy="branch")
@Where(clause = "branches_lft BETWEEN lft AND rgt")
private List<Product> products = new ArrayList<Product>();
public Collection getProducts() {
return products;
}
public void setProducts(Collection products) {
this.products = products;
}
}
I want to make bidirectional association bewteen those two tables with a custom select
Code:
SELECT FROM products, branches WHERE products.branches_lft BETWEEN branches.lft AND branches.rgt
but instead hibernate makes query like this
Code:
SELECT FROM products WHERE (branches_lft BETWEEN lft AND rgt) AND branches_lft = ?
Is it possible to do this in that or I have to write query or something?