Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: @OneToMany custom where clause
PostPosted: Mon Apr 16, 2012 3:53 pm 
Newbie

Joined: Mon Apr 16, 2012 3:51 pm
Posts: 1
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.