2010-10-06 117 views
2

我是一名hibernate初學者,在嘗試連接2個休眠表時出現問題。我想要做的是獲得某個商店取決於商店ID的產品列表,但是我得到的是每個商店下列出的數據庫中所有可用產品的列表。加入休眠狀態的外鍵

下面是Product.java代碼:

@Entity 
@Table (name = "products") 
public class Product implements Serializable{ 

/** 
* 
*/ 
private static final long serialVersionUID = -1001086120280322279L; 

@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column (name = "product_id") 
private int product_id; 

@Column(name = "product_name", unique=true) 
private String product_name; 

@JoinColumn(name = "store", referencedColumnName="store_id") 
@ManyToOne(cascade=CascadeType.ALL) 
private Store store; 

等。

,這裏是爲Store.java代碼:

@Entity 
@Table(name = "stores") 
public class Store implements Serializable{ 
/** 
* 
*/ 
private static final long serialVersionUID = 4497252090404342019L; 

@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column (name = "store_id") 
private int store_id; 

@Column(name = "store_name", unique=true) 
private String store_name; 

@JoinColumn(name="store", referencedColumnName= "store_id") 
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) 
private List<Product> productList; 

等。

下面是輸出:(產品A應在Butik A下,產品B在Butik下B)

Butik: Butik A 
Produkt: Banana A 
Produkt: Morot A 
Produkt: Banana B 
Produkt: Apple B 

Butik: Butik B 
Produkt: Banana A 
Produkt: Morot A 
Produkt: Banana B 
Produkt: Spple B 

我有2個附加的類,和的ProductDao該StoreDAO取查詢的護理,該代碼是在除了表名/類名這兩個類相似。

public class ProductDAO { 
    public static List<Product> getStoreProductsList() { 
    Session hibernateSession = HibernateUtil.getSession(); 
    hibernateSession.beginTransaction();  
    Query query = hibernateSession.createQuery("from Product"); 
    hibernateSession.getTransaction().commit();  
    List<Product> storeProducts = query.list(); 
    return storeProducts; 
    } 
} 

有沒有什麼辦法可以用hibernate解決這個問題?

感謝

+1

你如何檢索產品列表?你用什麼?加載?得到? Hibernate標準? HQL? SQL? – pakore 2010-10-06 09:21:01

+0

我有兩個額外的類,ProductDAO和StoreDAO負責查詢,代碼在兩個類中都是類似的,除了表名。公共類ProductDao這個{ \t公共靜態列表 getStoreProductsList(){ \t \t \t 會話\t = hibernateSession HibernateUtil.getSession(); \t \t hibernateSession.beginTransaction(); \t \t \t \t Query query = hibernateSession.createQuery(「from se.kyh.stores.entities.Product」); \t \t \t \t hibernateSession.getTransaction()。commit(); \t \t \t \t List storeProducts = query.list(); \t \t return storeProducts; \t \t } – pitic 2010-10-06 09:26:08

+0

您應該將此評論添加到原始問題中。試着編輯問題。 – 2010-10-06 09:41:06

回答

7

通過您的評論後。看起來你從來沒有在那裏設定條件,當然,你將最終得到所有的產品,不管他們屬於哪個商店。沒有驚喜。你在哪裏指定標準?

你可以做這樣的事情,

// One liner 
List<Product> list = session.createQuery("from Product p where p.store.store_id = " 
      +" :storeId").setInteger("storeId", storeId).list(); 

或者你可以得到Store,然後得到的Product就像下面的列表中,

// Another one liner 
List<Product> list = session.createCriteria(Store.class) 
      .add(Restrictions.eq("store_id", storeId)).list().getProductList(); 

另一種更簡單的方法,因爲我們知道STORE_ID是主鍵,(感謝Pakore提醒我)

// And another. Changed to use load() instead of get() here. 
// Assuming non-existance is an error. 
List<Product> list = (Store) session.load(Store.class,storeId).getProductList(); 

[編輯]

...添加兩個有用的指針(感謝Pascal

14.3 Associations and joins

14.4 Forms of join syntax

+1

+1。我只是添加一些鏈接到部分[14.3。關聯和連接](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-joins)和[14.4。連接語法形式](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql。html#queryhql-joins-forms)的文檔。 – 2010-10-06 13:07:02

+0

+1完整的答案與3種方式做它和學分:) – pakore 2010-10-06 13:43:04

+0

感謝你們的慷慨。 – 2010-10-07 04:47:45

4

問題是你的查詢,它只是選擇系統中的所有產品,無論商店。

嘗試使用hibernate標準。爲數據庫創建查詢更容易,並且始終可以從Java的「一側」而不是數據庫「一側」工作。

int storeid = 1 //whatever you want here. 
Criteria criteria = session.createCriteria(Product.class); 
criteria.add(Restrictions.eq("store_id",storeid)); 
List<Product> list = criteria.list(); 

list會註明是誰屬於Storestoreid等於你提供的storeid的產品清單。

但對於這個簡單的查詢,它甚至容易如果使用session.get()session.load()(它們之間的區別是在documentation關於他們這個answer或詳細信息)

int storeid = 1; //Whatever you want 
Store store = (Store) session.get(Store.class,storeid); 
List<Product> list = store.getProductList(); 

正如你看到的,休眠照顧你做的加入:)。

+1

你的第二個代碼片段很適合做一個小修改,你忘記了'list'的變量名。 +1。但是,您的查詢在第一個片段中將返回商店列表而非產品。所以,我希望你不介意解決這個問題。 :) – 2010-10-06 10:14:52

+0

Criteria API「更容易」是主觀IMO。我個人更喜歡HQL。從性能角度來看,HQL也更好。請參閱[Hibernate:Criteria與HQL](http://stackoverflow.com/questions/197474/hibernate-criteria-vs-hql)和[Hibernate查詢與條件性能](http://stackoverflow.com/questions/1784631 /休眠查詢-VS-標準性能)。 – 2010-10-06 13:17:28

+0

@Adeel固定,該死的迷你重構:D。 @帕斯卡爾,確實是主觀的。我認爲對於初學者和簡單的查詢更容易使用'Criteria',它更加面向Java。 HQL更多的是QL導向。感謝您的鏈接,但我不知道性能差異! – pakore 2010-10-06 13:39:26