samedi 14 mars 2015

How to create hibernate criteria query for multiple levels


Vote count:

0




I am trying to design a query using hibernate criteria API. The goal is to get a filtered list of items stored at 3rd level in form of java.util.Set which match a particular value. Further the list should be filtered at the outer most level for another specific value. Below are the model classes that form a multi level relationship:


Seller.java



@Entity
@Table(name="seller")
public class Seller {
private Integer id;
private Set<Store> stores;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", unique=true, nullable=false)
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}

@ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinTable(name="seller_store",
joinColumns={@JoinColumn(name="sellertid")},
inverseJoinColumns={@JoinColumn(name="storeid", referencedColumnName="id")})
@ForeignKey(name="fk_sellerid_seller_store", inverseName="fk_storeid_seller_store")
public Set<Store> getStores() {
return stores;
}
public void setStores(Set<Store> stores) {
this.stores = stores;
}
}


Store.java



@Entity
@Table(name="store")
public class Store {
private Integer id;
private Stock stock;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", unique=true, nullable=false)
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}

@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval=true)
@JoinColumn(name="stockid", referencedColumnName="id")
@ForeignKey(name="fk_stockid_store")
public Stock getStock() {
return stock;
}
public void setStock(Stock stock) {
this.stock = stock;
}
}


Stock.java



@Entity
@Table(name="stock")
public class Stock {
private Integer id;
private Set<Item> stockItems;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", unique=true, nullable=false)
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}

@ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinTable(name="stock_stockitem",
joinColumns={@JoinColumn(name="stockid")},
inverseJoinColumns={@JoinColumn(name="stockitemid")})
@ForeignKey(name="fk_stockid_stock", inverseName="fk_stockitemid_stock")
public Set<Item> getStockItems() {
return stockItems;
}
public void setStockItems(Set<Item> stockItems) {
this.stockItems = stockItems;
}


}


Item.java



@Entity
@Table(name="item")
public class Item {
private Integer id;
private String name;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", unique=true, nullable=false)
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}

@Column(name="name", unique=true)
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}


Now I need to get a specific Stock object containing Items with a particular id and that too for an a Seller with a particular id. I could easily do this by getting the top level Seller object by id but that will put a burden on my code to manually filtering generating list of Stock with multiple for loops which definitely is not a good idea. Any suggestions and guidelines for mapping and/or criteria logic will be most appreciated. Thanks in advance!



asked 1 min ago







How to create hibernate criteria query for multiple levels

Aucun commentaire:

Enregistrer un commentaire